?
Solved

Reading from a txt files line by line

Posted on 2004-09-26
4
Medium Priority
?
239 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 100 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 150 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

862 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