I am doing a encryption project in C for my school project.I got everything working except a small bug.When I read the input text file,my program encrypts the EOF line, so it adds an unwanted character to my output file.This happens because in UNIX each text file has an invisible new line at the end,which my program reads and tries to encode .can any body tell me how can i not read that last new line ?
thanks
How to not read end of the file character when reading a file in C?
There are a few ways you can go, so here are a few --
If you can get the file size, you can read the file in a loop and stop after that number of bytes instead of relying on the EOF.
Or go ahead and read every byte and just check to see if is is the EOF character before it gets encoded and stored.
Reply:If you know what character it is, you should be able to exclude it. for example, when reading in the text file, lets say you are doing it character by character:
while (myfile.get(ch)){
if (ch != char (2) ) texttoencrypt[x]=ch;
x++;
}
By doing the char (2) you would eliminate the END of TEXT character. www.asciitable.com will tell you what each character's value is, and you can try exluding different ones.
Reply:I'm not sure exactly what you're asking. You've mentioned a few different things.
The end of file character, at least for input, was CTRL-Z (26). End of line is different - PCs use 0x0D 0x0A (CR/LF), while unix usually just uses 0x0A.
What you can do is restructure your reading so that you don't write the old line until you've read the new line and if the new line is the end of the file, then you can trim the old line before writing it.
Alternatively you can just blindly go forth and write, but when you hit the end of the file you'll have to seek back and remove the line terminator if that was the end of it.
If your problem is the newline characters, you'll have to parse each line as it comes in and strip off the char you don't want, either by writing a 0 over it, or a 0x0A 0x00 (to get rid of the 0x0D) and write it out.
If you can word your question a bit better, I can be a bit clearer on what you need to do.
Reply:Do you know with certainty that each file you have has a newline at the end? It is common to *NIX systems to terminate a text file with a newline, especially code files, _but it isn't necessary_. The newline at the end isn't invisible. It's plainly visible, and they are included because certain programs require a text file to have such a terminating newline.
I guess what you could do is attempt to read in two lines at once. Two fgets as the constraint in a while loop would make sense. There's a few cases you need to handle though. The while loop exists if either one hits EOF. If it's the first one, you're done so no worries. If it's the second fgets that fails, you have a problem. It's possible the first fgets obtained the terminating newline, in which case no worries. You don't need to do any processing within the loop. But if the file wasn't null terminated, clearly, you need to encrypt that last line.
So be sure to check after the while loop that the first fgets either failed or it obtained a newline.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment