| | |
Reading data from a textfiletype
Thread Solved
![]() |
•
•
Join Date: Feb 2007
Posts: 30
Reputation:
Solved Threads: 0
Im trying to read some integers values from a text file.
The problem is that first lines of the text files contains some text and only then theres the values that i want to read into variables , and i dont know how to get down three lines and then read using fscanf .
TEXT FILE(storeinfo.txt):
Stores Information:
Store Code Store Size Store Workers Store Income
951 1200 7 67000
952 1452 12 92833
953 800 5 41000
954 1000 6 61000
955 1500 12 100321
Code:
The problem is that first lines of the text files contains some text and only then theres the values that i want to read into variables , and i dont know how to get down three lines and then read using fscanf .
TEXT FILE(storeinfo.txt):
Stores Information:
Store Code Store Size Store Workers Store Income
951 1200 7 67000
952 1452 12 92833
953 800 5 41000
954 1000 6 61000
955 1500 12 100321
Code:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> typedef struct { int code; int size; int workers; int total; }store; int main() { int i; FILE *filein; char source[40]; store storearr[5]={0}; printf ("Enter a source filename: "); gets(source); filein = fopen (source,"rt"); if (filein == NULL) { printf ("Unable to open sourcefile...\n"); exit(1); } for (i=0;i<5;i++) fscanf(filein,"%d %d %d %d\n",&storearr[i].code,&storearr[i].size,&storearr[i].workers,&storearr[i].total); for (i=0;i<5;i++) printf ("%-15d %-15d %-15d %-15d\n",storearr[i].code,storearr[i].size,storearr[i].workers,storearr[i].total); fclose (filein); return 0; }
Keep a counter which will be initialized to 1,
Read this for using
fgets the entire line of text in a character array, skip the first two lines using the counter previously mentioned and start reading from the third line.Read this for using
fgets. Last edited by ~s.o.s~; Apr 7th, 2007 at 6:52 am.
I don't accept change; I don't deserve to live.
#include <stdio.h>
#include <stdlib.h>
#define MYFILE "c:\\example.txt"
int main(void)
{
FILE *fp;
char buf[BUFSIZ] = "blah";
int i;
if ((fp = fopen(MYFILE, "r")) == NULL)
{
perror (MYFILE);
return (EXIT_FAILURE);
}
i = 0;
while (fgets(buf, sizeof(buf), fp) != NULL)
{
if (i==0 || i == 1)
printf("Skip these\n");
else
printf ("Line %2d: %s", i, buf);
i++;
}
fclose(fp);
getchar();
return(0);
} Last edited by iamthwee; Apr 7th, 2007 at 8:33 am.
*Voted best profile in the world*
![]() |
Similar Threads
- Reading in data into array (special case, please see) (C++)
- reading in data (char, int.. etc) without return key (C)
- Perl/CGI (Reading Data) Part II (Computer Science)
- Reading data into files (C++)
- Multiple data being read in (C++)
- Reading Keystates (C)
Other Threads in the C Forum
- Previous Thread: type conversion
- Next Thread: moving folder
| Thread Tools | Search this Thread |
* ansi array asterisks binarysearch calculate changingto char character cm convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createprocess() database feet fflush fgets file floatingpointvalidation fork forloop function givemetehcodez global grade gtkwinlinux hacking histogram homework i/o inches infiniteloop input interest intmain() iso kernel keyboard kilometer km linked linkedlist linux locate looping loopinsideloop. lowest match meter microsoft mqqueue number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv recvblocked research reversing scanf segmentationfault sequential single socket socketprograming socketprogramming standard string suggestions systemcall threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windowsapi






