Thread: Scanning of filename_i.dat files with i=1:N in a C code.

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    1

    Scanning of filename_i.dat files with i=1:N in a C code.

    hello experts,
    I have a quick question regarding the scanning a certain amount of .dat files. What I have is a list of table as follow:

    S809_1.dat
    S809_2.dat
    S809_3.dat
    S809_4.dat

    made of 4 columns of floating elements as you can see in the attachment.


    What I want to do now is to automate the process, because I want to import N similar .dat files. So, I want to import N table made like:

    S809_1.dat
    S809_2.dat
    S809_3.dat
    S809_4.dat
    ...
    ...
    ...
    S809_N.dat

    Is there a fast way to do it? or do I have to import table by table using N fscanf in the same way I was doing?

    Thank you in advance.
    Attached Images Attached Images Scanning of filename_i.dat files with i=1:N in a C code.-screenshot-2019-08-22-18-20-23-png 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Next time, please post your code between code tags.
    Fuzzy pictures are useless for copy/pasting your work into a fixed up example.

    So, briefly
    Code:
    #define N 100
    int main ( ) {
      struct something Re[N+1] = { 0 };  // your struct, initialised
      for ( int i = 1 ; i <= N ; i++ ) {
        char filename[100];
        sprintf(filename,"S809_%d.dat",i);
        FILE *fp = fopen(filename,"r");
        // your while loop, adjusting Re1 to Re[i]
        fclose(fp);
      }
    }
    But watch out if your N * sizeof(struct something) is very large (like more than 1MB).

    If N isn't a compile time constant, say so.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanning numbers in files into arrays
    By pepelepew in forum C Programming
    Replies: 3
    Last Post: 11-12-2009, 05:24 AM
  2. need some help in scanning files in database
    By d4xyjen in forum C Programming
    Replies: 3
    Last Post: 08-15-2009, 05:13 PM
  3. scanning text files
    By deleeuw in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2002, 03:18 PM
  4. header files and code files..
    By CompiledMonkey in forum C++ Programming
    Replies: 4
    Last Post: 02-15-2002, 09:35 AM

Tags for this Thread