943,923 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 70873
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 5th, 2007
0

read each line of file

Expand Post »
I have a question:
I have a file named "data.inp". This file have numbers:
6 3 5 4 5
2 5 3 1
5 8 7 9
2 5 6 3 5 8 4 6
If I want more line of numbers, I will add numbers.
But I don't know how to read file "data.inp" line by line. I think I should use fgets, but seem that fgets is used for char. Or I used fgetc to read all numbers, then I count '\n' (note: "enter"), add 1, I have number of line. Then maybe I read file by that way?
thanks in advance!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
donaldunca is offline Offline
27 posts
since Sep 2006
Feb 5th, 2007
0

Re: read each line of file

fgets() doesn't know the difference between characters, such as 'a', 'b', 'c' ... and numbers such as '1', '2', '3' ... '9'. In the file they are all just text. If you want to treat all those numbers as text, read the file line by line, then just use fgets() the normal way
  1. char line[80]
  2. FILE* fp = fopen("data.inp","r");
  3. while(fgets(line,1,sizeof(line),fp)
  4. {
  5. // do something
  6. }
  7. fclose(fp);

If you just want to add more lines to that file, then it isn't necessary to read it at all. Open the file for append and start writing -- new lines will be appended to the end of the existing lines.
Last edited by Ancient Dragon; Feb 5th, 2007 at 9:38 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 5th, 2007
0

Re: read each line of file

Click to Expand / Collapse  Quote originally posted by donaldunca ...
But I don't know how to read file "data.inp" line by line. I think I should use fgets, but seem that fgets is used for char.
Yes, use fgets() to read the line, then you can use sscanf() to convert the line into numbers.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,738 posts
since May 2006
Feb 5th, 2007
0

Re: read each line of file

I don't know whether or not this may help:
http://www.daniweb.com/code/printsnippet444.html
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 6th, 2007
0

Re: read each line of file

If I caculate sum of numbers line by line.
This file "data.inp" have numbers:
6 3 5 4 5
2 5 3 1
5 8 7 9
2 5 6 3 5 8 4 6
I want the result is:
23
11
29
39
(Note: 29= 6 +3 +5 +4 +5
11= 2 +5+ 3+ 1
29= 5 +8 +7 +9
39= 2 +5 +6 +3 +5 +8+ 4+ 6)
thanks!
Reputation Points: 10
Solved Threads: 0
Light Poster
donaldunca is offline Offline
27 posts
since Sep 2006
Feb 6th, 2007
0

Re: read each line of file

OK. So use the information from my post and think about how you'd have to do it.

Or read the link from Dave. Two possible ways to handle this.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,738 posts
since May 2006
Feb 12th, 2007
0

Re: read each line of file

I try but I can't run my program.
Have EOL (end of line) in C?
I know only EOF. I used fgets but it read for char
Reputation Points: 10
Solved Threads: 0
Light Poster
donaldunca is offline Offline
27 posts
since Sep 2006
Feb 12th, 2007
0

Re: read each line of file

Post your code so that we can atleast point out the mistakes you are making...
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Feb 12th, 2007
0

Re: read each line of file

Click to Expand / Collapse  Quote originally posted by donaldunca ...
I try but I can't run my program.
Have EOL (end of line) in C?
I know only EOF. I used fgets but it read for char
Stay away from EOF, use the examples as shown.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 14th, 2007
0

Re: read each line of file

Here is my code :

#include<conio.h>
#include<stdio.h>
main()
{
    clrscr();
    int v[10],v1[10],i=0,j,t,k,number,count=0;
    FILE *f ;
    f=fopen("numbers.inp","r");
/* open file numbers.inp to read, for e.g, you inputs two rows ( can be more than two ) of number 
    5 4 4 3 3 3 2
    2 2 1 
*/
     do
     {
        i++;
        fscanf(f,"%d",&v[i]);
        count++;
     }
/* after these above commands, I change all chars in numbers.inp to integers belong to array v[i] so I think 5 4 4 3 3 3 2 and 2 2 1 now are integer not char */
     while((number=getc(f))!=EOF);
/* I think problem is here, when I use fscanf I lost two rows and now
it read to end of file and it considers my two rows as one row v[i]={5 4 4 3 3 3 2 2 2 1} and it apply the beneath algorithm for it.
What I want is it does the algorithm to the first row 5 4 4 3 3 3 2 and export result to result.out, then does with the second row 2 2 1 and export result to result.out */
     k=count;
     while(v[1]>0)
     {
        for(i=2;i<=(v[1]+1);i++)
            v1[i-1]=v[i]-1;
        for(i=(v[1]+2);i<=count;i++)
            v1[i-1]=v[i];
        count--;
        for(i=1;i<=count;i++)
            v[i]=v1[i];
        for(i=1;i<=(count-1);i++)
        for(j=(i+1);j<=count;j++)
        if(v[i]<v[j])
        {
            t=v[i];
            v[i]=v[j];
            v[j]=t;
        }
        for(i=1;i<=count;i++)
        {
            printf("%2d",v[i]);
            if(i==count)     printf("\n");
        }
        k--;
     }
     f=fopen("result.out","w");
     i=k;
     if(v[i]>(k-1)||(v[i]<0)) fprintf(f,"NO");
     if(v[i]==0) fprintf(f,"YES");
     getch();
     fclose(f);
    }
Hope you understand me.
Reputation Points: 10
Solved Threads: 0
Light Poster
donaldunca is offline Offline
27 posts
since Sep 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: how do u find prime numbers in an array
Next Thread in C Forum Timeline: turboC





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC