fscanf to read lines from file?

C

CJ

Newbie here.

Trying to tackle following:
Read text file containing list of files delimited by end of line
chars.

I need to somehow write a loop that filters out each of the lines of
text within the text file as a filename to which I can then apply a
number of functions.

I've thought of:
FILE *into_filePointer;
char file_name[256];

into_filePointer = (<my-textfile>, "r");
fscanf (into_filePointer);

.... that's where I lack understanding... How can I draw out line by
line.
I'd like to do a loop:
while (into_filePointer != EOF)
create filename1
.....
on next loop
create filename2

etc.

Any help would be appreciated.
 
F

FMorales

CJ said:
Newbie here.

Trying to tackle following:
Read text file containing list of files delimited by end of line
chars.

I'm going to assume this means a '\n' character.

If your file is in the format of:

Line1\n
Line2\n
Line3\n

each \n being where you hit ENTER, i'd suggest the 'fgets' function.

char * fgets( char * buff, int size, FILE * stream );

One thing i've used often, is gotten the length of the longest line
in the file, then allocated a buffer big enough to hold that line.
That way i could hold /any/ line from the file just fine. The
nice thing about fgets is if it reaches a '\n' character /before/
'size' bytes are read in, it will copy the '\n' into 'buff', and
append a null character ('\0'). So long as you allocated enough
space for 'buff' you should be fine. In my case i had the ease of
knowing no 'users' would randomly change the contents of my file.
If your file names can grow in length at any time i'd suggest some
other way to read up to a '\n'. Could get slightly more complex,
especially if you start getting into reallocating 'buff'. Or if you
have the ability to recompile the code to reflect those name changes,
such as i did, that's another option as well. One thing to be sure you
keep in mind is allocating enough room for the longest string + 1. That
way you'll have enough for fgets to copy that '\n' character over, AND
append the null terminating character. Otherwise it could get ugly.

Just an idea i've used that's worked nicely, hope it helps.
FMorales...
 
D

Dan Pop

In said:
Newbie here.

Trying to tackle following:
Read text file containing list of files delimited by end of line
chars.

I need to somehow write a loop that filters out each of the lines of
text within the text file as a filename to which I can then apply a
number of functions.

I've thought of:
FILE *into_filePointer;
char file_name[256];

into_filePointer = (<my-textfile>, "r");
fscanf (into_filePointer);

... that's where I lack understanding... How can I draw out line by
line.

Ever considered reading a C book?

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top