473,466 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problems when reading from a file with fscanf

Hi,

I'm getting some problems when using fscanf to read a file.

This is a piece of the program code:

main ()
{
/*** Variable declaration ***/
FILE *vpfile; /*** Data file ***/

struct line
{
char old_al[10];
char new_al[10];
char vp[5];
char new_dl[17];
};
struct line l[1000]={0};
int i=0;
int nl=0; /*** number of lines read ***/
int res=0;

/*** Executable statements ***/
/*** Open VpMove.txt file ***/
vpfile=fopen("/export/home/granite/CAPI/prog/bin/VpMove.txt","r");
if(vpfile==NULL)
{
printf("The file VpMove.txt could not be opened\n");
return;
}
/*** Read datafile ***/
while(fscanf(vpfile,"%s",l[i].old_al)!=EOF)
{
fscanf(vpfile,"%s",l[i].new_al);
fscanf(vpfile,"%s",l[i].vp);
fscanf(vpfile,"%s",l[i].new_dl);
i++;
}
nl=i-1; /*** undo i++ ***/
for(i=0;i<=nl;i++)
{
printf("Line %d %s\t %s\t %s\t
%s\n",i,l[i].old_al,l[i].new_al,l[i].vp,l[i].new_dl);
}
fclose(vpfile);
}

The VpMove.txt file looks like this:
003360073 003362061 240 AP-B_LIE_PP01-523
003360073 003351154 244 AP-B_LIE_PP01-10
003360073 003351154 243 AP-B_LIE_PP01

The output is the following:
Line 0 003360073 003362061 240 AP-B_LIE_PP01-523003360073
Line 1 003360073 003351154 244 AP-B_LIE_PP01-10
Line 2 003360073 003351154 243 AP-B_LIE_PP01

The problem is that when the word ends with a minus sign followed by 3
digits (see first line) he takes directly the following word: so
instead of AP-B_LIE_PP01-523 I receive AP-B_LIE_PP01-523003360073.

Can someone help me on this one?????
Nov 14 '05 #1
3 6714
In article <d7**************************@posting.google.com >,
Benedicte <be******@hotmail.com> wrote:
Hi,

I'm getting some problems when using fscanf to read a file.

This is a piece of the program code:

It's great you posted a working program, but please rember to
also include apropriate headers, e.g.: 'stdio.h' in this case.
main ()
{
/*** Variable declaration ***/
FILE *vpfile; /*** Data file ***/

struct line
{
char old_al[10];
char new_al[10];
char vp[5];
char new_dl[17];
};
struct line l[1000]={0};
int i=0;
int nl=0; /*** number of lines read ***/
int res=0;

/*** Executable statements ***/
/*** Open VpMove.txt file ***/
vpfile=fopen("/export/home/granite/CAPI/prog/bin/VpMove.txt","r");
if(vpfile==NULL)
{
printf("The file VpMove.txt could not be opened\n");
return;
}
/*** Read datafile ***/
while(fscanf(vpfile,"%s",l[i].old_al)!=EOF)
{
fscanf(vpfile,"%s",l[i].new_al);
fscanf(vpfile,"%s",l[i].vp);
fscanf(vpfile,"%s",l[i].new_dl);
i++;
}
nl=i-1; /*** undo i++ ***/
for(i=0;i<=nl;i++)
{
printf("Line %d %s\t %s\t %s\t
%s\n",i,l[i].old_al,l[i].new_al,l[i].vp,l[i].new_dl);
}
fclose(vpfile);
}

The VpMove.txt file looks like this:
003360073 003362061 240 AP-B_LIE_PP01-523
003360073 003351154 244 AP-B_LIE_PP01-10
003360073 003351154 243 AP-B_LIE_PP01

The output is the following:
Line 0 003360073 003362061 240 AP-B_LIE_PP01-523003360073
Line 1 003360073 003351154 244 AP-B_LIE_PP01-10
Line 2 003360073 003351154 243 AP-B_LIE_PP01

The problem is that when the word ends with a minus sign followed by 3
digits (see first line) he takes directly the following word: so
instead of AP-B_LIE_PP01-523 I receive AP-B_LIE_PP01-523003360073.

Can someone help me on this one?????


The problem is that you overflow the character array of 'new_dl' fields.
This array is 17 characterss big, 'AP-B_LIE_PP01-523' has 17 characters,
leaving no space for the '\0' (zero terminator) that fscanf(...) wants
to insert at the end. When you print the value of 'new_dl' you run into
UB since printf expects a zero terminator to end strings. You might
find this zero terminator at the first field of the next record, that
explains the '003360073' part.
Increase the size of 'new_dl' from 17 to 18 and you'll see it works,
this is of course by no means a constructive solution.

Regards,
--
Rob van der Leek | rob(at)ricardis(dot)tudelft(dot)nl
Ricardishof 73-A | http://www.ricardis.tudelft.nl/~rob
2614 JE Delft, The Netherlands
+31 (0)6 155 244 60
Nov 14 '05 #2
Benedicte wrote:

struct line
{
char old_al[10];
char new_al[10];
char vp[5];
char new_dl[17];
};
So you have allocated 17 charecters for new_dl
fscanf(vpfile,"%s",l[i].new_dl);
fscanf has wrote 18 charecters to new_dl. So undefined behavior.
Really it has wrote ending '\0' to the old_al of the second array member.
nl=i-1; /*** undo i++ ***/
for(i=0;i<=nl;i++)
{


for (i = 0; i < nl; i++)
is consider to be standart among programmers. So use it insted of
decreasing nl

Using scanf is not safe because of buffer overrun as in your example.
You can use fgets and break the line into tokens by strtok or strpbrk
If you don't like fgets you can use some thing like:

char *afgets (char *s, size_t *lenp, FILE *f)
{
char *t;
ptrdiff_t tmp;
size_t l = 0;

if (lenp)
l = *lenp;
if (!s)
l = 0;
if (l == 0) {
l = 10;
s = malloc (l);
}
t = s;
for (;;) {
fgets (t, l, f);
t += strlen (t) - 1;
if (*t == '\n' || feof (f))
break;
tmp = t - s;
if ((t = realloc (s, l *= 2)) == NULL) {
l /= 2;
break;
}
s = t;
t += tmp;
}
if (lenp)
*lenp = l;
return s;
}

Nov 14 '05 #3


Benedicte wrote:
Hi,

I'm getting some problems when using fscanf to read a file.

This is a piece of the program code: #include <stdio.h>
main () int main(void)
{
/*** Variable declaration ***/
FILE *vpfile; /*** Data file ***/

struct line
{
char old_al[10];
char new_al[10];
char vp[5];
char new_dl[17];
};
You have not made some of the character arrays in the struct large
enough for the data. Looking at the data you supplied:

The VpMove.txt file looks like this:
003360073 003362061 240 AP-B_LIE_PP01-523
003360073 003351154 244 AP-B_LIE_PP01-10
003360073 003351154 243 AP-B_LIE_PP01

The struct, at a minimum should be:

struct line
{
char old_al[11]; /* allows enough for the string ending '\0' char */
char new_al[11];
char vp[4];
char new_dl[18];
};
struct line l[1000]={0};
int i=0;
int nl=0; /*** number of lines read ***/
int res=0;
What is res used for?
/*** Executable statements ***/
/*** Open VpMove.txt file ***/
vpfile=fopen("/export/home/granite/CAPI/prog/bin/VpMove.txt","r");
if(vpfile==NULL)
{
printf("The file VpMove.txt could not be opened\n");
return;
main returns an int. You could make this:
return 1; }
/*** Read datafile ***/
while(fscanf(vpfile,"%s",l[i].old_al)!=EOF)
{
fscanf(vpfile,"%s",l[i].new_al);
fscanf(vpfile,"%s",l[i].vp);
fscanf(vpfile,"%s",l[i].new_dl);
i++;
}
nl=i-1; /*** undo i++ ***/
for(i=0;i<=nl;i++)
{
printf("Line %d %s\t %s\t %s\t
%s\n",i,l[i].old_al,l[i].new_al,l[i].vp,l[i].new_dl);
}
You can modify the above to:

for(nl = 0; 4 == fscanf(vpfile, " %s %s %s %s ",l[nl].old_al,
l[nl].new_al, l[nl].vp, l[nl].new_dl) ; nl++) ;
for(i=0;i < nl;i++)
printf("Line %d %s\t %s\t %s\t %s\n",
i,l[i].old_al,l[i].new_al,l[i].vp,l[i].new_dl);
fclose(vpfile);
return 0; }


--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Colin JN Breame | last post by:
Hi, Fairly new to C. What is the best way to read a line (\n terminated) from a file? Ive looked at fscanf but was not sure which format specifier to use. (%s perhaps). Thanks Colin
1
by: bas | last post by:
hey, I am having a lot of trouble trying to get this program to work. It is supposed to read integers from a file and place them into the categories for inventory. The problem seems to be that...
6
by: bas | last post by:
hey, I am having a lot of trouble trying to get this program to work properly. It is supposed to read integers from a file and place them into the categories for inventory. The problem seems to...
29
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and...
4
by: John | last post by:
I need to read data from the file like the following with name and score, but some line may only has name without score: joe 100 amy 80 may Here's my code, but it couldn't read the line with...
10
by: lancer6238 | last post by:
Hi all, I'm having programs reading from files. I have a text file "files.txt" that contains the names of the files to be opened, i.e. the contents of files.txt are Homo_sapiens.fa...
2
by: ajay0419 | last post by:
Hi: I am trying to read from a file, which stores answers for a particular question(The answers are stop listed ). Each answer in the file is enclosed in opening and closing braces. Here is the...
5
by: imailz | last post by:
Hi all, since I'm forced to switch from Fortran to C I wonder if there is posibility in C: 1) to use implicit loops 2) to parse several variables which number is determined at runtime. ...
3
by: godblessingme | last post by:
Hi, I'm getting some problems when using fscanf to read a file. This is a piece of the program code: #include <stdlib.h> #include <stdio.h> int main() {
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.