formatting fscanf

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tooswole23
    New Member
    • Feb 2012
    • 10

    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 | ----- -------- - -------- - -------- - -------- - | --- ---------- - ---------- - --------- - --------- - --------- - ---------- -
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • tooswole23
      New Member
      • Feb 2012
      • 10

      #3
      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?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        That would be:
        Code:
        fscanf(d,"%s %s %s %s %s",&a,&b,&c,&d,&e);
        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.
        Last edited by weaknessforcats; Feb 27 '12, 02:42 AM. Reason: error in code

        Comment

        • tooswole23
          New Member
          • Feb 2012
          • 10

          #5
          Code:
          #include "stdafx.h"
          #include<conio.h>
          #include<string.h>
          #include <stdlib.h>
          
          struct StarInfo
          {
          	char name [25];
          	char RA[13];
          char Dec[13];
          char irac1[13];
          char irac2[13];
          char irac3[13];
          char irac4[13];
          char mips[13];
          char qual[6];
          char Erirac1[13];
          char Erirac2[13];
          char Erirac3[13];
          char Erirac4[13];
          char Ermips[13];
          };
          
          void main(const char*file)
          {
          struct StarInfo star[3];
          int StarNumber=0;
          
          printf("Number of stars to find the best fit for?");//max 3
          scanf("%d",&StarNumber);
          char *search=" ";
          
          FILE *d;
          
          d=fopen("t.dat","rb");
          
          if(d==NULL)
          {
          printf("The file was not read correctly, type an integer to close");
          scanf("%d", &StarNumber);
          return;
          }
          
          for(int counter=0; counter<StarNumber; counter++)
          {
          printf("The file was read correctly... Now inputing data of star number %d \n", counter+1);
          
          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);
          }	
          
          int a=0;
          printf("Show data of which star?");
          scanf("%d",&a);
          
          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);
          	
          }
          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

          Comment

          • soniya
            New Member
            • Feb 2012
            • 1

            #6
            formatting the fscanf get the sscanf and scanf

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              When you ask to print the first star, are you asking for 1 or 0? The first star is 0.

              Comment

              • tooswole23
                New Member
                • Feb 2012
                • 10

                #8
                I'm asking for the 0th star.

                Comment

                Working...