Crazy fgets stuff [modified]
-
I am using this code:
FILE * pFile; char filename [100]; pFile = fopen ("\\Settings\\temp.txt" , "rt"); if (pFile == NULL) perror ("Error opening file"); else { fgets (filename , 100 , pFile); fclose (pFile); }
to get a filename that another part of my program has written. However whatever I put in the text file my program reads 100 Í characters. Now when I open up the text file myself it shows exactly what I want, i.e. not that :) This is driving me slightly crazy so any help would be great. P.S. If anyone wants the text file I am quite willing to e-mail it to you. -- modified at 10:15 Wednesday 23rd August, 2006 -
I am using this code:
FILE * pFile; char filename [100]; pFile = fopen ("\\Settings\\temp.txt" , "rt"); if (pFile == NULL) perror ("Error opening file"); else { fgets (filename , 100 , pFile); fclose (pFile); }
to get a filename that another part of my program has written. However whatever I put in the text file my program reads 100 Í characters. Now when I open up the text file myself it shows exactly what I want, i.e. not that :) This is driving me slightly crazy so any help would be great. P.S. If anyone wants the text file I am quite willing to e-mail it to you. -- modified at 10:15 Wednesday 23rd August, 2006You said another part of your program is writing the file? Here are a few things to try: - Initialize your string:
char filename[100] = {0};
- Set the file cursor to the beginning explicitly:fseekg(pFile, 0, SEEK_SET);
- Make sure that the file is not currently open by the section of code that is suppose to be writing to it. You may be running into a race condition if you have 2 threads (for example) where thread 1 is writing to the file, but thread 2 is already trying to read from it before the write is complete.If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
I am using this code:
FILE * pFile; char filename [100]; pFile = fopen ("\\Settings\\temp.txt" , "rt"); if (pFile == NULL) perror ("Error opening file"); else { fgets (filename , 100 , pFile); fclose (pFile); }
to get a filename that another part of my program has written. However whatever I put in the text file my program reads 100 Í characters. Now when I open up the text file myself it shows exactly what I want, i.e. not that :) This is driving me slightly crazy so any help would be great. P.S. If anyone wants the text file I am quite willing to e-mail it to you. -- modified at 10:15 Wednesday 23rd August, 2006 -
You said another part of your program is writing the file? Here are a few things to try: - Initialize your string:
char filename[100] = {0};
- Set the file cursor to the beginning explicitly:fseekg(pFile, 0, SEEK_SET);
- Make sure that the file is not currently open by the section of code that is suppose to be writing to it. You may be running into a race condition if you have 2 threads (for example) where thread 1 is writing to the file, but thread 2 is already trying to read from it before the write is complete.If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
Right, I have made sure that the other program isn't writing to the file. Initializing the char just results in the program returning a blank string. Using fseek doesn't seem to make any difference. Do you think it could be a problem with the format of the file?
-
Ever tried with: pFile = fopen ("\\Settings\\temp.txt" , "r");
~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
-
How do you it returns only a bunch of Ì ? Is this in the debugger ? Because the snippet you've provided is almost the example of the use of the
fgets
function in the MSDN, and it should work properly. Have you tested it with another temp file ?~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
-
I am using this code:
FILE * pFile; char filename [100]; pFile = fopen ("\\Settings\\temp.txt" , "rt"); if (pFile == NULL) perror ("Error opening file"); else { fgets (filename , 100 , pFile); fclose (pFile); }
to get a filename that another part of my program has written. However whatever I put in the text file my program reads 100 Í characters. Now when I open up the text file myself it shows exactly what I want, i.e. not that :) This is driving me slightly crazy so any help would be great. P.S. If anyone wants the text file I am quite willing to e-mail it to you. -- modified at 10:15 Wednesday 23rd August, 2006stevelam wrote:
fgets (filename , 100 , pFile);
chage that to if( fgets (filename , 100 , pFile) == NULL ) { //ERROR } this prevent your prog from crashing. there is nothing wrong with that part of your code the file could have been created in binary mode. the buffer in this case filename could be currupted after the read op.
G_S
-
How do you it returns only a bunch of Ì ? Is this in the debugger ? Because the snippet you've provided is almost the example of the use of the
fgets
function in the MSDN, and it should work properly. Have you tested it with another temp file ?~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
-
Right, I have made sure that the other program isn't writing to the file. Initializing the char just results in the program returning a blank string. Using fseek doesn't seem to make any difference. Do you think it could be a problem with the format of the file?
stevelam wrote:
Initializing the char just results in the program returning a blank string
That means that fgets isn't reading in anything for some reason. Check its return value to see if it is getting an error.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
I am using this code:
FILE * pFile; char filename [100]; pFile = fopen ("\\Settings\\temp.txt" , "rt"); if (pFile == NULL) perror ("Error opening file"); else { fgets (filename , 100 , pFile); fclose (pFile); }
to get a filename that another part of my program has written. However whatever I put in the text file my program reads 100 Í characters. Now when I open up the text file myself it shows exactly what I want, i.e. not that :) This is driving me slightly crazy so any help would be great. P.S. If anyone wants the text file I am quite willing to e-mail it to you. -- modified at 10:15 Wednesday 23rd August, 2006what don't you do that in a C++ way ? STL is full of good things you know
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]