Solved

Reading from a txt files line by line

Posted on 2004-09-26
4
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 how to use strings and some functions related to them 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.

696 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