Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fopen question - beginner c programmer

Status
Not open for further replies.

thagan

Programmer
Aug 30, 2001
1
US
I have a txt file with unix files listed in it. I need to get the file names in that file, and open those files for some processing.
I can open the txt file ok, but when I use the "obtained" string in a subsequent fopen call, it fails with error code 2 (NOENT).
snippet:
[
FILE *F;
char filename[256];
f = fopen("list.txt", "r");
if(f != NULL)
{
fgets(filename, 256, f);
printf("%s", filename); -- prints: /tmp/my_c_file.c
fclose(f);
}
else
return -1;
f = fopen(filename, "rb+"); -- fails
]
Any hints?
Thanks in advance.
 
When you read the file name from a file as you have the string will contain a <CR><LF> therefore when you try to open the file using that string, the open statement will look for the terminating NULL and see a <CR><LF>. You will want to be sure that the filename looks like this in memory

filename.txt<NULL> not filename.txt<CR><LF><NULL>

to see what the string really looks like, perform a loop through the string and print each byte as both the ASCII representation and the HEX representation or if you use an IDE that allows you to view memory...take a look at it that way
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top