Thread: Reading, Storing and Writing a file in C

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    11

    Reading, Storing and Writing a file in C

    Hello,

    So this is the task i am trying to complete. Take a text file and read it into C. read specifically the last 3 columns of the file and store them as arrays for later calculation. How can i just scan those particular columns and bypass the rest. Attached is the text file

    I also wanted to write a file of just those 3 columns to as a check that i have done it correctly

    This is what i started writing as a general set up

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <quadmath.h>
    typedef __float128 real;
    typedef __float128 doublereal;
    #include <stdlib.h>
    
    
    double LDout[81], LDout2[81], LDout3[81]; 
    
    FILE *fptr1, *fptr2;
    
    //Main function
    int main()
    {
    
    //Open/Reads file Surrogate Run_Finite_Difference_81.in
        fptr1 = fopen("Surrogate Run_Finite_Difference_81.in","r"); 
    
    //Open/Writes file Surrogate Run_Finite_Difference_81.out
    
        fptr2 = fopen("Surrogate Run_Finite_Difference_81.out","w");  
    
    
    
    
    //Close Finite_Difference.in file
        fclose(fptr1);
    //Close Finite_Difference.out file
        fclose(fptr2);
    return 0;
    }


    I am fairly new to C so any help or sudo code would be appreciated.
    Attached Files Attached Files

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    Hi,

    I tried to answer this about 8 hours ago but there was a glitch in the matrix. The glitch is now gone (thanks Salam & laserlight for looking into it).

    I guess that I'd look at doing something like this (but modify it to read floating point values and ignore the correct number of leading columns of course)

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        const char *in_str = "1 2 3 4";  /* pretend this is a row in the text file */
        int a, b;
        
        int conversion_count = sscanf(in_str, "%*d %*d %d %d", &a, &b);
        
        printf("%d conversions: %d and %d\n", conversion_count, a, b);
        
        return 0;
    }
    Now that I can see the text file you'll clearly need to change the %d to %lf or %f but you get the general idea I hope. The * between the % and the conversion specifier (e.g. %*lf) means read the value but ignore it (essentially). Compile and run the test code above and it'll make sense. There is also %n but I don't think you'll need it in your case. I dunno how many columns are in that text file but just add enough %*lf conversion specifiers to ignore/skip everything except for the last 3 columns. Clearly there are other ways to approach this problem (e.g. maybe a loop) but this approach might suffice although it might be better to use a loop to ignore the stuff you're not interested in

    Edit: Hmm. You'll have to do something else for the first column but I need to go out. See how far you can get and get back to us... maybe someone else will respond before I get home
    Last edited by Hodor; 01-07-2020 at 03:38 AM.

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    11
    Hey! thank you for the tip! I extracted what i need to exactly! For now this will suffice so i can move on with other calculation. But i do want to put it into a loop so it looks cleaner but im not to sure how i'll do that yet. If you have any more tips that would be great! Thank you again



    Code:
    #include <stdio.h>
    #include <math.h>
    #include <quadmath.h>
    typedef __float128 real;
    typedef __float128 doublereal;
    #include <stdlib.h>
    
    char   line[1500];
    int    i;
    double LDout[80], LDout2[80], LDout3[80];
    
    int main()
    {
    fptr3 = fopen("Surrogate Run_Finite_Difference_81.in","r");   //Open/Reads file Surrogate Run_Finite_Difference_81.in
        fptr4 = fopen("Surrogate Run_Finite_Difference_81.out","w");  //Open/Writes file Surrogate Run_Finite_Difference_81.out
    
    
        printf("Input:\n\n");      //print to screen
    
    
    // Reads the LD labels
        fscanf(fptr3, "%s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %s %s %s\n", line, LDout, LDout2, LDout3);
        fprintf(fptr4,"%s %s %s %s\n", line, LDout, LDout2, LDout3);
        printf("%s %s %s %s\n", line, LDout, LDout2, LDout3);
    
    
    // Reads the Values for LDout, LDout2 and LDout3 from Surrogate Run_Finite_Difference_81.in
        for (i=0; i<=80; i++) {
            fscanf(fptr3, "%s %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %*lf %lf %lf %lf\n", Doe, &LDout[i], &LDout2[i], &LDout3[i]);
            fprintf(fptr4,"%s %.16lg %.16lg %.16lg\n", Doe, LDout[i], LDout2[i], LDout3[i]);
            printf("%s %.16lg %.16lg %.16lg\n", Doe, LDout[i], LDout2[i], LDout3[i]);
        }

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    Not exactly how I'd do it, and hopefully I counted the number of columns correctly, but maybe something like the code below. I'll leave it to you to add error checking (check for end of file etc etc). The numbers 80 and 41 probably shouldn't be hardcoded, it'd be better to have #define SKIP_COLUMN_COUNT 41 and then use j < SKIP_COLUMN_COUNT in the loop, for example

    Code:
    #include <stdio.h>
    #include <errno.h>
    
    int main(void)
    {
        char   line[1500];
        int    i;
        double LDout, LDout2, LDout3;
        
        FILE *fptr3 = fopen("Surrogate Run_Finite_Difference_81.in","r");   //Open/Reads file Surrogate Run_Finite_Difference_81.in
        if (!fptr3) {
            perror("Error");
            return 1;
        }
        
        fgets(line, sizeof line, fptr3);    // Read the labels
            
        for (i = 0; i <= 80; i++) {
            for (int j = 0; j < 41; j++) {
                fscanf(fptr3, " %*s");
            }
            fscanf(fptr3, " %lf %lf %lf", &LDout, &LDout2, &LDout3);
            printf("%f %f %f\n", LDout, LDout2, LDout3);
        }
        
        fclose(fptr3);
        
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2020
    Posts
    11
    Hello, you have helped me tremendously with this forum. I do have another question if you would be able to assist? would be greatly appreciated. Link is below. Its a add on to what i was doing here. The j term is not necessarily 41, it could be a lower value so i have to determine this be counting the number of columns from the text file.

    How to count the amount of columns or elements in my text file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from file, storing it in a table
    By Rootwing in forum C Programming
    Replies: 2
    Last Post: 03-25-2016, 08:49 PM
  2. Storing a value in an array from reading in from a file.
    By calCOOLus in forum C Programming
    Replies: 4
    Last Post: 09-12-2013, 11:57 PM
  3. Reading a file and storing it as a 2d Array.
    By zimbomac in forum C Programming
    Replies: 16
    Last Post: 08-24-2012, 07:43 AM
  4. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  5. reading file and storing to arrays
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 05:17 AM

Tags for this Thread