473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. int main()
  4. {
  5. //variable declaration
  6. double position[1000][2]={0};
  7.  
  8. FILE *fp;int j;
  9.  
  10. fp=fopen("1.txt","r");
  11.  
  12.  
  13. char array[100];
  14.  
  15. while(!feof(fp))
  16.  
  17. {
  18.  
  19.  
  20. fgets(array,100,fp);
  21.  
  22. //skip the comment lines which begins with '#' 
  23.  
  24. if(strncmp(array,"#",1)!=0)
  25.  
  26. {
  27. //read from data files with fscanf
  28.  
  29. for(j=1;j<30;j++)
  30.  
  31. {
  32.  
  33.  
  34. fscanf(fp,"%lf, %lf, %lf %*[^\n]",&position[j][0],&position[j][1],&position[j][2]);
  35.  
  36.  
  37. }//for(j=0;j<1000;j++)       
  38.  
  39.  
  40. }//if(strncmp(array[i],####,1)!=0)
  41.  
  42.  
  43. }//while(!feof(fp))
  44.  
  45. fclose(fp);
  46.  
  47. //print points out
  48. for(j=1;j<7;j++)
  49. {
  50. printf("Point %d is %lf %lf %lf\n", j,position[j][0],position[j][1],position[j][2]);
  51.  
  52. }//for(j=1;j<7;j++)
  53. }
The 1.txt file looks like this:
#DATA
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:
Point 1 003360073 003362061 240 AP-B_LIE_PP01-523003360073
Point 2 003360073 003351154 244 AP-B_LIE_PP01-10
Point 3 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?????
Oct 7 '08 #1
3 1971
gpraghuram
1,275 Expert 1GB
Can you check the regular expression pattern with scanf.
I think that is causing the issue

Raghu
Oct 8 '08 #2
newb16
687 512MB
How can code like this
Expand|Select|Wrap|Line Numbers
  1. printf("Point %d is %lf %lf %lf\n", j,position[j][0],position[j][1],position[j][2]);
  2.  
print something like this
Expand|Select|Wrap|Line Numbers
  1. Point 3 003360073 003351154 243 AP-B_LIE_PP01
  2.  
Where is 'is' after 'Point %d' and how does %lf specifier print ..._LIE_... ?
Oct 8 '08 #3
Tassos Souris
152 100+
Well it seems that you have 2 errors in your code...

1st Error : you miss the first line of the input file
2nd Error : you get some weird string on output

Oh, you also need to #include <string.h> if you haven't already done that..

As far as the 1st error is concerned, examine how your code executes:
--> Read the #DATA... it starts with # so skip the for loop
--> Read 003360073, 003362061, 240 #AP-B_LIE_PP01-523.. it does not start
with # so go into the for loop.. but in the for loop you read AGAIN, thus you miss the previous you read... so input 003360073, 003362061, 240 #AP-B_LIE_PP01-52 goes wasted!!

As for the second error i think that it happens due to a common error and happens during input.. it is very simple actually.. well you store 3 numbers in your array's "columns"... so why you made it 2???
Change
Expand|Select|Wrap|Line Numbers
  1. double position[1000][2];
  2.  
To:
Expand|Select|Wrap|Line Numbers
  1. double position[1000][3];
  2.  
One last thing...
In the printf statement you do not need the lf... actually this is undefined behavior..
In fprintf format l goes with d,i, o, u,x,X specifying that it applies to a long int or unsigned long int argument and to n specifying that it applies to a pointer to long int.
To e,E,f,g,G L can be applied that specifies that they apply to long double argument rather than double.
So, you do not need the l at all.

Hope i helped..
Let us now if you fixed the problem
Oct 8 '08 #4

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

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
3
by: Benedicte | last post by:
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 ***/
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...
9
by: EkteGjetost | last post by:
I would like to first apologize to those of you who read my last post "desperately need help". As a regular on other forums i can understand how aggravating it would be to have someone come on who...
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. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.