hallo
i want 2 know if there is a function that reads from an input file and after reading this char will disapear.
i mean the read char will b disappeared.

Dani AI

Generated

Files don't lose bytes when you read them — a file can only be changed by writing. As noted, to make the input become "100" after reading "ack=100" you must rewrite the file. You have two practical choices: parse the value and leave the file unchanged (simpler and cheap), or rewrite the file to remove the prefix (what you asked for). pointed out parsing options; was right that rewriting is the general solution.

A safe, common pattern for removing a leading token is: open the original for reading, verify and skip the prefix you expect, stream the rest into a temporary file, close both streams, then atomically replace the original with the temp. That avoids loading big files into memory and protects the original if something goes wrong.

Example (stream-copy, skip a prefix at start):

#include <fstream>
#include <cstdio>
#include <string>

void removePrefixAtStart(const char* filename, const std::string& prefix) {
    std::ifstream in(filename, std::ios::binary);
    if (!in) return;
    std::string head(prefix.size(), '\0');
    in.read(&head[0], head.size());
    if (!in) return; // I/O error
    if (head == prefix) {
        std::ofstream out("tmpfile.tmp", std::ios::binary);
        out << in.rdbuf(); // copy remainder
        in.close();
        out.close();
        std::remove(filename);
        std::rename("tmpfile.tmp", filename);
    }
    // else: prefix not found at file start — take no action or handle as needed
}

Notes and troubleshooting: always work on a copy when testing; check return codes and stream states; use a temporary filename on the same filesystem to make rename atomic; handle permissions and concurrent access; if the prefix can be elsewhere in the file or variable in length, either read-and-edit in memory (small files) or scan while copying to the temp file to skip the matching bytes.

Recommended Answers

All 10 Replies

Member Avatar for Member #46692

hallo
i want 2 know if there is a function that reads from an input file and after reading this char will disapear.
i mean the read char will b disappeared.

If not you could just write your own.

or can i read and write in the same file

Yes. You CAN read and wrte in the same file. You will have to use fstream to declare your stream.

http://www.daniweb.com/tutorials/tutorial6542.html

You will have to read the file into memory, change it there and then write it back if you want the read character to be deleted.

how i can read the file in memory
i want the char 2 b saved in memory not a copy

You have to copy everything after your character into an array or something, then come back and overwrite the character. Did you get me?

I think you need a clearer definition of what you want. characters don't just "disappear" -- unless your a magician.

Post an example of what you want. For example, if the word "hello" is read, do you want to delete the two 'l's and make it "heo"?

for ex:
here is my input file ack=100 , and i want 2 read the four chars ack= and then after reading the input file will b as shown 100

Member Avatar for Member #46692

for ex:
here is my input file ack=100 , and i want 2 read the four chars ack= and then after reading the input file will b as shown 100

I don't no what you mean? :sad:
What is ack?

>then after reading the input file will b as shown 100
Files are persistent, they don't work like that. If you want a destructive read, you'll need to build it yourself by rewriting the file. Since that's an expensive operation, you'll want to seriously consider whether you really need this feature or not.

for ex:
here is my input file ack=100 , and i want 2 read the four chars ack= and then after reading the input file will b as shown 100

If I understand you, you want to put the value 100 into an integer variable:?: If that is true, you have a couple options:
(1) use sscanf(), or (2) pointer parsing. There are other options in c++ using stringstream class.

char buf[20];
int n;
// read a line into buf is not shown
//buf[] = "ack=100"; after read
//
sscanf(buf,"ack=%d",&n); // now n == 100

(2) parse with a pointer

char buf[20];
char* ptr;
int n;
// read a line into buf is not shown
//buf[] = "ack=100"; after read
//
ptr = strchr(buf,'='); // locate the '=' symbol
n = atoi(ptr+1); // convert remainder to int
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.