473,321 Members | 1,778 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,321 software developers and data experts.

formatting fscanf

I am trying to use fscanf to obtain a set of 14 or so strings per line, in a line where there are around 80 or so different sets of strings. I only need the first 14 and whenever I call scan f it starts at column 209 as opposed to column 1 where it should. Here's a sample of the code:

FILE *d;

d=fopen("t.dat","rb");

where a, n are all strings.

fscanf(d,"%s %s %s %s %s %s %s %s %s %s %s %s %s %s",&a,&b,&c,..(etc)..,&n);

A sample of the Input file looks like this:
USB270.15385-29.63146 270.153847 -29.631455 2.966699e+03 -9.99 1.300391e+03 -9.99 -9.99 A-A-- 6.787463e+01 -9.99 1.555773e+02 -9.99 -9.99 10100 | ----- ------ ------ ------ | 0.373 13.554 12.928 12.670 AAA | ----- -------- - -------- - -------- - -------- - | --- ---------- - ---------- - --------- - --------- - --------- - ---------- -
Feb 25 '12 #1

✓ answered by weaknessforcats

That would be:
Expand|Select|Wrap|Line Numbers
  1. fscanf(d,"%s %s %s %s %s",&a,&b,&c,&d,&e);
  2.  
But this assumes you have just opened the file or did a seek() to the beginning. Otherwise, this code just read the next 5 strings.

Note that these strings must be contiguous and not consecutive. Contiguous means nothing between the strngs.

Again, please note, you have to know where the strings are. fscanf cannot fish them out for you. If there are other values between the strings, then you need to read them also to position yourself correctly in the file.

7 2497
weaknessforcats
9,208 Expert Mod 8TB
fscanf doesn't know about column 1. Your parameter string tells it what to do. %s%s%s%s says to read the next four strings. If you call fscanf again with a %s it will read the fifth string. All whitespace, and that includes \n are ignored.

With all disc files, it is required you know the file layout.
Feb 25 '12 #2
In that case, how would I format it to take in the data for the first consecutive 5 strings and insert them into strings a to e?
Feb 27 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
That would be:
Expand|Select|Wrap|Line Numbers
  1. fscanf(d,"%s %s %s %s %s",&a,&b,&c,&d,&e);
  2.  
But this assumes you have just opened the file or did a seek() to the beginning. Otherwise, this code just read the next 5 strings.

Note that these strings must be contiguous and not consecutive. Contiguous means nothing between the strngs.

Again, please note, you have to know where the strings are. fscanf cannot fish them out for you. If there are other values between the strings, then you need to read them also to position yourself correctly in the file.
Feb 27 '12 #4
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include<conio.h>
  3. #include<string.h>
  4. #include <stdlib.h>
  5.  
  6. struct StarInfo
  7. {
  8.     char name [25];
  9.     char RA[13];
  10. char Dec[13];
  11. char irac1[13];
  12. char irac2[13];
  13. char irac3[13];
  14. char irac4[13];
  15. char mips[13];
  16. char qual[6];
  17. char Erirac1[13];
  18. char Erirac2[13];
  19. char Erirac3[13];
  20. char Erirac4[13];
  21. char Ermips[13];
  22. };
  23.  
  24. void main(const char*file)
  25. {
  26. struct StarInfo star[3];
  27. int StarNumber=0;
  28.  
  29. printf("Number of stars to find the best fit for?");//max 3
  30. scanf("%d",&StarNumber);
  31. char *search=" ";
  32.  
  33. FILE *d;
  34.  
  35. d=fopen("t.dat","rb");
  36.  
  37. if(d==NULL)
  38. {
  39. printf("The file was not read correctly, type an integer to close");
  40. scanf("%d", &StarNumber);
  41. return;
  42. }
  43.  
  44. for(int counter=0; counter<StarNumber; counter++)
  45. {
  46. printf("The file was read correctly... Now inputing data of star number %d \n", counter+1);
  47.  
  48. fscanf(d,"%s %s %s %s %s %s %s %s %s %s %s %s %s", &star[counter].RA, &star[counter].Dec, &star[counter].irac1,  &star[counter].irac2,  &star[counter].irac3,  &star[counter].irac4,  &star[counter].mips,  &star[counter].qual,  &star[counter].Erirac1, &star[counter].Erirac2,  &star[counter].Erirac3,  &star[counter].Erirac4,  &star[counter].Ermips);
  49. }    
  50.  
  51. int a=0;
  52. printf("Show data of which star?");
  53. scanf("%d",&a);
  54.  
  55. printf("Name:%s \n RA: %s \n Dec: %s\n Irac1: %s\n Irac2: %s\n Irac3: %s\n Irac4: %s\n Mips: %s\n qual: %s\n ErIrac1: %s\n ErIrac2: %s\n ErIrac3: %s\n ErIrac4: %s\n ErMips %s\n", &star[a].name, &star[a].RA, &star[a].Dec, &star[a].irac1, &star[a].irac2, &star[a].irac3, &star[a].irac4, &star[a].mips, &star[a].qual, &star[a].Erirac1,&star[a].Erirac2, &star[a].Erirac3, &star[a].Erirac4, &star[a].Ermips);
  56.  
  57. }
  58.  
I've been testing the code for the first star, and the way I have it set up it should just read the first 14 strings of the line with the fscanf, unless I'm mistaken.
It returns 14 consecutive strings, but starting on the 13th string as opposed to the the 1st string
Feb 27 '12 #5
soniya
1
formatting the fscanf get the sscanf and scanf
Feb 27 '12 #6
weaknessforcats
9,208 Expert Mod 8TB
When you ask to print the first star, are you asking for 1 or 0? The first star is 0.
Feb 27 '12 #7
I'm asking for the 0th star.
Feb 27 '12 #8

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

Similar topics

22
by: John Phung | last post by:
Is there a fscanf equivalent for c++? Here's what I'm talking about: unsigned int NextAddress(ifstream& AddressFile) { unsigned int next_address; -->How do I rewrite the fscanf listed below...
2
by: Blankdraw | last post by:
.... somewhere, a newbie is dying ... Is there anybody out there who can help me get the right input for the following segment? I am trying to read entire records of 5 (2-digit) integers at a...
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 ***/
4
by: Psibur | last post by:
Hello, trying to get back into c and was having issue with reading a simple text file with an aribtrary # of lines with 3 int's per line, with the eventual purpose of putting each int into an...
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
1
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
9
by: quyvle | last post by:
I can't seem to get this function to work correctly. I'm wondering if anyone could help me out with this. So I'm using the fscanf function to read the input stream and store each string in the...
13
by: PeterOut | last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2). I am not sure if this is a C, C++ or MS issue but fscanf has been randomly hanging on me. I make the call hundreds, if not thousands, of...
59
by: David Mathog | last post by:
Apologies if this is in the FAQ. I looked, but didn't find it. In a particular program the input read from a file is supposed to be: + 100 200 name1 - 101 201 name2 It is parsed by reading...
42
by: Bill Cunningham | last post by:
I'm doing something wrong and all I know to do is turn to clc. I have a text file containing 2 doubles separated by a tab. ..26 0 Is the text. I want to read the two double and printf them...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.