RSS Forums RSS

read each line of file

Please support our C advertiser: Programming Forums
Thread Solved
Reply
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

read each line of file

  #1  
Feb 5th, 2007
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!
AddThis Social Bookmark Button
Reply With Quote  
Posts: 13,885
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1231
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: read each line of file

  #2  
Feb 5th, 2007
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
char line[80]
FILE* fp = fopen("data.inp","r");
while(fgets(line,1,sizeof(line),fp)
{
   // do something
}
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 8:38 am.
Reply With Quote  
Posts: 3,026
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 267
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: read each line of file

  #3  
Feb 5th, 2007
Originally Posted by donaldunca View Post
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.
Sometimes, when I look at my children, I say to myself, "Lillian, you should have remained a virgin."
-- Lillian Carter (mother of Jimmy Carter)
Reply With Quote  
Posts: 3,877
Reputation: Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold 
Solved Threads: 164
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: read each line of file

  #4  
Feb 5th, 2007
I don't know whether or not this may help:
http://www.daniweb.com/code/printsnippet444.html
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote  
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

Re: read each line of file

  #5  
Feb 6th, 2007
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!
Reply With Quote  
Posts: 3,026
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 267
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: read each line of file

  #6  
Feb 6th, 2007
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.
Sometimes, when I look at my children, I say to myself, "Lillian, you should have remained a virgin."
-- Lillian Carter (mother of Jimmy Carter)
Reply With Quote  
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

Re: read each line of file

  #7  
Feb 12th, 2007
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
Reply With Quote  
Posts: 7,398
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 439
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: read each line of file

  #8  
Feb 12th, 2007
Post your code so that we can atleast point out the mistakes you are making...
I don't accept change; I don't deserve to live.

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.

-- Eric Naggum RIP :-(
Reply With Quote  
Posts: 5,068
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 355
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: read each line of file

  #9  
Feb 12th, 2007
Originally Posted by donaldunca View Post
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.
*Voted best profile in the world*
Reply With Quote  
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

Re: read each line of file

  #10  
Feb 14th, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 15853 | Replies: 17 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:28 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC