473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

fscanf to read lines from file?

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.
Nov 14 '05 #1
2 2665
CJ wrote:
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.

---

Any help would be appreciated.


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...

Nov 14 '05 #2
In <a7**************************@posting.google.com > ca********@hotmail.com (CJ) writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Benedicte | last post by:
Hi, I'm getting some problems when using fscanf to read a file. This is a piece of the program code: main () { /*** Variable declaration ***/ FILE *vpfile; /*** Data file ***/
4
by: Psibur | last post by:
Hello, trying to get back into c and was having issue with reading a simple text file with an aribtrary # of lines with 3 int's per line, with the eventual purpose of putting each int into an...
7
by: Kay | last post by:
1) If i want to read data from a txt file, eg John; 23; a Mary; 16; i How can I read the above data stopping reading b4 each semi-colon and save it in three different variables ? 2) If I...
5
by: learner | last post by:
I have datafiles like this: 0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.02 0.00 0.00 1 0 1941 0.00 0.03 0.00 0.03 0.04 0.02 0.00 0.00 0.00 0.00 2 0 1941 0.00 0.00 0.00 0.00 0.52...
1
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
7
by: bhanuprakash | last post by:
I am trying to use fscanf to read my test file. In my test file i sometimes have blank lines. When I try to read using the following format. fscanf(fp,"%\n",temp_str); If there is any blank...
4
by: John | last post by:
I need to read data from the file like the following with name and score, but some line may only has name without score: joe 100 amy 80 may Here's my code, but it couldn't read the line with...
37
by: PeterOut | last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2). I am not sure if this is a C, C++ or MS issue but fscanf has been randomly hanging on me. I make the call hundreds, if not thousands, of...
10
by: rsk | last post by:
Hi Friends, I have written a code which suppose to read all the numbers from a hex file,But to my surprise the code is skiping every alternate value.Don't know why? Can you please help me in...
5
by: a | last post by:
After reading FAQ comp.lang.c section 12 and googling again, still there is no threads talking about reading a series of numbers. The input files, somehow structured, is exemplified below: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.