Solved

Reading from a txt files line by line

Posted on 2004-09-26
4
232 Views
Last Modified: 2010-04-15
hi there

im trying to read the following

1 1 1 3 3
2 4 4 8 8
3 2 3 6 5
4 5 4 8 2
5 4 5 8 8
6 1 4 2 6
7 4 5 3 2

the first nummber on everyline is the line number and the other 4 integers are integers i need to get.  

this is what i have until now  but im sure this is wronge becouse it display the whole file.

------------------------

#include <stdio.h>

int main(void)
{
FILE *fp;
char ch;

fp = fopen("list.txt","r");
ch = getc(fp);
while(ch!=EOF)
{
putchar(ch);
ch = getc(fp);
}
printf("\n\n");
}
--------------------------------------
my numbers are int.  

thanks in advance
Kimos

0
Comment
Question by:kimos123
  • 2
4 Comments
 
LVL 2

Assisted Solution

by:fulp02
fulp02 earned 50 total points
ID: 12154423
If you want to read the text file line by line you have to keep reading the file untill you get the "\n" escape
sequence
an easier way to do this would be to use the fgets function


heres a link showing you what to do

http://www.iota-six.co.uk/c/i2_feof_fgets_fread.asp
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 75 total points
ID: 12154555
Just need to use fscanf, it is the easiest way, something like this

int main(void)
{
FILE *fp;
/* define variable to receive values */
int d1,d2,d3,d4;


fp = fopen("list.txt","r");
while(!feof(fp))   /* loop until end of file */
{
    fscanf(fp, "%i %i %i %i", &d1, &d2, &d3, &d4);   /* take a look to scanf and fscanf documentation */

    printf("%i %i %i\n", d2, d3, d4);  /* print 2nd to 4th numbers */
}
printf("\n\n");

return 0;
}
0
 

Author Comment

by:kimos123
ID: 12154757
i used this method


 while(!feof(file)) {
      /* loop through and store the numbers into the array */
      fscanf(file, "%d", &numbers[i]);
      i++;
    }

and i modified the txt file

1 1 3 3
4 4 8 8
2 3 6 5
5 4 8 2
4 5 8 8
1 4 2 6
4 5 3 2

the next problem is:
i want to get the integers separeted and print them with a for loop that can  print all the integers like this
first line ((1,1)(3,3))  
second line etc...

this way my progamm dosnt depent on howmany lines in the list are

i hope u understand the problem

thanks
0
 

Author Comment

by:kimos123
ID: 12155064
never mind i got it with

printf("\nrechthoek  = %d , %d , %d , %d ",numbers[k],numbers[k+1],numbers[k+2],numbers[k+3]);

k=k+3;

in the for loop

thanks for the help
0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question