Solved

Reading from a txt files line by line

Posted on 2004-09-26
4
229 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
FILE closing 11 68
Compile VxWorks Toronado project under Visual Studio 11 161
UPD maximums on Red Hat 6 102
convert char array to number in c 5 77
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now