468,484 Members | 1,792 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

Reading a text file

This is a input file

DEL 0050 XYZ 0050 0310 CCU 0710
MAA 1325 TRZ 10415
KWI 0050 FJR 0325 0325 CCJ
KWI 0050 FJR 0325 0425 CCJ 0925 1005

i.e maximum items(column entries) in a row can be 8
but their data type will be in same order
i.e char int char int int char int int

i am simply trying to read it
here's my code


#include<stdio.h>
#include<string.h>
main()
{
FILE *fp;
struct air
{
char a[50];
int b;
char c[50];
int d;
int e;
char f[50];
int g;
int h;
};
struct air ia;

fp=fopen("SC.txt","rb");
if(fp==NULL)
{
puts("Cannot open file");
exit(0);
}

while(fscanf (fp,"%s %d %s %d %d %s %d %d\n",ia.a,&ia.b,ia.c,&ia.d,&ia.e,ia.f,&ia.g,&ia.h ) != EOF)
{
printf("%s %d %s %d %d %s %d %d\n",ia.a,ia.b,ia.c,ia.d,ia.e,ia.f,ia.g,ia.h);
}

fclose(fp);

}

But the output contains garbage value...
it is reads the1st line till 7th column enrty but after that it prints garbage value
similarly it reads the 2nd line till 4th entry but after that it prints garbage value
but in last line it is showing the entire line...
please help me...i need to read each element in file cuz i have to use and compare these characters with some value.
Jul 26 '06 #1
4 14292
Banfa
9,055 Expert Mod 8TB
Don't use fscanf.

You have told it to read 8 values but some of the lines do not contain 8 values. Then you try to output all 8 values whether or not 8 values have been read so on the lines with less than 8 values you get the garbage that was already in the variable in question as there was nothing to read from the file. Additionally fscanf is fraught with buffer overrun dangers, especially when reading a string.

Use fgets (and don't bother opening the file in binary mode, text mode should be OK i.e. "r" not "rb"). This reads the line into a string but makes sure you can not overwrite the end of the string.

Then use a pointer(or 2) and the function strtol to scan down the line reading in the data and counting the number of data points at the same time. Once you know how many data points are on the line set default values for the data points not present and only output what you have actually read.
Jul 26 '06 #2
This is a input file

DEL 0050 XYZ 0050 0310 CCU 0710
MAA 1325 TRZ 10415
KWI 0050 FJR 0325 0325 CCJ
KWI 0050 FJR 0325 0425 CCJ 0925 1005

i.e maximum items(column entries) in a row can be 8
but their data type will be in same order
i.e char int char int int char int int

i am simply trying to read it
here's my code


#include<stdio.h>
#include<string.h>
main()
{
FILE *fp;
struct air
{
char a[50];
int b;
char c[50];
int d;
int e;
char f[50];
int g;
int h;
};
struct air ia;

fp=fopen("SC.txt","rb");
if(fp==NULL)
{
puts("Cannot open file");
exit(0);
}

while(fscanf (fp,"%s %d %s %d %d %s %d %d\n",ia.a,&ia.b,ia.c,&ia.d,&ia.e,ia.f,&ia.g,&ia.h ) != EOF)
{
printf("%s %d %s %d %d %s %d %d\n",ia.a,ia.b,ia.c,ia.d,ia.e,ia.f,ia.g,ia.h);
}

fclose(fp);

}

But the output contains garbage value...
it is reads the1st line till 7th column enrty but after that it prints garbage value
similarly it reads the 2nd line till 4th entry but after that it prints garbage value
but in last line it is showing the entire line...
please help me...i need to read each element in file cuz i have to use and compare these characters with some value.

Hi,
The garbage values are getting printed becoz of the reason what Bafna has mentioned. To avoid this u can try fgetc( ); function in a while loop and fetch the characters & store it in an array until u receive the ascii value of the keyboard key 'ENTER'. The ENTER key value will indicate that the string has come to an end. As u recv the ENTER value, append null in the array.
Jul 27 '06 #3
Thanks alot to all you guys...

The problem just got solved...

so once again thanks aton to all you who helped me out...and to those you read my query/problem.

Bye
Jul 28 '06 #4
Hi all,

I had written a code to read text file and I can already run it. Now, I want to expand my code so that, after reading that file, it can insert the contents of the file to the table that had been created in MySQL. Hope anyone can help me on how to do it..
Thanks

p/s: I'm using C
Aug 1 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by fabrice | last post: by
19 posts views Thread by Lionel B | last post: by
50 posts views Thread by Michael Mair | last post: by
2 posts views Thread by Sabin Finateanu | last post: by
4 posts views Thread by Amit Maheshwari | last post: by
3 posts views Thread by The Cool Giraffe | last post: by
2 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.