Thread: Read data from txt file with headers

  1. #1
    Registered User
    Join Date
    Sep 2021
    Posts
    3

    Question Read data from txt file with headers

    Hi,

    I have a txt file (attached) that I am attempting to read data from and store into an array. The data has headers and I am having trouble skipping the headers and only reading the data. What is the best way to go about this?

    Thanks.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Quote Originally Posted by Oly View Post
    Hi,

    I have a txt file (attached) that I am attempting to read data from and store into an array. The data has headers and I am having trouble skipping the headers and only reading the data. What is the best way to go about this?

    Thanks.
    What have you tried? Do you have any code that you can show?

    My advice would be to use getline() to read the first line then discard/no put the line into any use.

    Code:
               getline(arguments);
    
               //read the rest of the file.
    To me discarding the header in the data.txt would probably be the best solution.

  3. #3
    Registered User
    Join Date
    Sep 2021
    Posts
    3
    I've tried the following, and it works when the headers are removed from the txt file. I'm just unsure how to modify it to work with headers.

    Code:
        for(i=0; i<BUFSIZE; ++i)
        {
            if(fscanf(f, "%f,%f,%f,%f", &x[i][0], &x[i][1], &x[i][2], &x[i][3]) != 4)
                break;
            n++;
        }
        
    
        fclose(f);
    
        for(i=0; i<n; ++i){
            printf("[ %f%f%f%f ]\n", x[i][0], x[i][1], x[i][2], x[i][3]);
    
        }
    

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Probably the easiest thing to do is to use fgets instead of fscanf
    Code:
    char buff[BUFSIZ];
    while ( fgets(buff, BUFSIZ, f) ) {
        if(sscanf(buff, "%f,%f,%f,%f", &x[n][0], &x[n][1], &x[n][2], &x[n][3]) == 4)
            n++;
    }
    The attempt to sscanf the header line will fail, but you've read it from the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2021
    Posts
    3
    That worked perfectly, thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read Input file, Calculate data, Write Output file
    By Ron09sime in forum C Programming
    Replies: 10
    Last Post: 07-04-2021, 02:17 AM
  2. Replies: 3
    Last Post: 03-13-2013, 07:10 PM
  3. Replies: 21
    Last Post: 11-03-2007, 02:56 PM
  4. how can i read data from file... plz help me
    By fnfn in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2007, 06:39 AM
  5. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 09:37 AM

Tags for this Thread