Thread: Scanning and Displaying Data File

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    10

    Scanning and Displaying Data File

    Hey guys,

    I'm trying to scan 3 columns of data from a .txt file. One of the columns does not contain some data. Below is an example of the .txt file.

    Code:
    1	5	
    2	6	
    3	7	
    4	7	
    5	8	2
    6	9	5
    7	0	6
    8	2	27
    9	1	8
    10	5	9
    11	67	0
    12	8	1
    The problem is when I try to display it in the program, it places a value in those blank cells. For example, the first row is "1 5 blank" but the program will display it as "1 5 2" because it places the "3rd" value in that cell. How can I make it so it skips it?

    Here is my code thus far:

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
    #include <stdlib.h>
    
    int main(void){
                     
         FILE *infile, *outfile;
         float xdata[99], ydata[99], zdata[99];
         int lcv;
         
         infile = fopen("data.txt", "r");
         
         if (infile == NULL){
         printf("unable to open file");
                    
         } else {
                           
         for(lcv=0; lcv <=21; ++lcv){
                    fscanf(infile, "%f %f %f",&xdata[lcv],&ydata[lcv],&zdata[lcv]); //read
                    printf("%10.0f %10.2f %10.2f\n",xdata[lcv],ydata[lcv],zdata[lcv]);                
                    }
                    fclose(infile);
    }        
    }
    Last edited by MetaMorphicX; 01-08-2009 at 02:15 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Use fgets to read in one line, and then use sscanf to read out of that line.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    Use fgets to read in one line, and then use sscanf to read out of that line.
    And the return value from sscanf() to know how many values you actually got.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    10
    Hey,

    Thanks for the help. I'm pretty new to programming and would appreciate an example of how to use those functions in my case. I tried looking them up but still can't get a good grasp. Thanks.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As you may have noticed, looking around, code does not magically appear here very often. If you have questions about how the functions work, then ask those questions. Ideally would be a snippet of code, what you think the code should do, and why, and we can go from there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying Progress.
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 04-11-2008, 03:56 PM