Solved

Reading from a txt files line by line

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

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…
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 opening and writing to files 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.

863 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

27 Experts available now in Live!

Get 1:1 Help Now