473,626 Members | 3,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems when reading from a file with fscanf

1 New Member
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 1984
gpraghuram
1,275 Recognized Expert Top Contributor
Can you check the regular expression pattern with scanf.
I think that is causing the issue

Raghu
Oct 8 '08 #2
newb16
687 Contributor
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 New Member
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
9567
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
6729
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
1451
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 the program is only reading the first line of numbers from the text file and printing them in ever category. Any help or suggestions woulud be great, I swear i probably tried everything #include <stdio.h>
6
1780
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 be that the program will only read the first line of integers from the file, and just print them into all the categories. Any help or suggestions woulud be great, I swear i probably tried everything #include <stdio.h>
9
2106
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 obviously doesn't know the community and asks for people to do their work for them. So i've come much more prepared this time. What my problem is, is that i need to write a program that will count the number of alphabetic characters, numbers,...
29
10403
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 then print out the contents of the 2d array to the screen. I wil also need to append data to the .dat file but mostly right now I'm worrying about getting the info into the 2d array. My .dat file looks like this 1 20000 2 30000 3 40000
4
4224
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 "may" because there is no score. Anyone knows what is the workaround to this problem?
10
3168
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 Rattus_norvegicus.fa (They are FA files that can be opened in any text editor.)
2
1470
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 sample file { tenets nutrigenomics jim kaput improper diets risk factors disease dietary chemicals alter gene expression andor genome diseases } { individuals nutritional needs a result own genetic } I am trying to read from this file and...
5
5039
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. Following example: The output contains n columns which have to be read in. The number of
0
8262
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8701
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7192
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5571
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2623
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 we have to send another system
1
1807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.