Reading data from consecutively named files

This is a discussion on Reading data from consecutively named files within the C Programming forums, part of the General Programming Boards category; Hello, first post here so i hope i don't break any rules. I need to read some data from several ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    16

    Reading data from consecutively named files

    Hello, first post here so i hope i don't break any rules.


    I need to read some data from several consecutively named data files. The thing is a) the data files are gonna be named something like: datafile0001.asc, datafile0002.asc, etc, that is the way they're named and b) they will have random names, say one set will start with 820nm500mw0001.asc , another set will start with 830nm500uw, another will start with 780100mw0001.asc, i.e. their names are random.


    This program below works if the file name is inputted as datafile, without the 0001.asc, i.e. if only the prefix is inputted. For 780100mw0001.asc, i need to input 780100mw when the program asks for the original filename.

    So, i'd want the program to work by inputting the datafile name completely i.e. 780100mw0001.asc.

    Any ideas on how to do this?

    TIA

    Code:
    #include <stdio.h> 
    #include <math.h> 
    #include <stdlib.h> 
    #include <string.h> 
    
    
    
    int main(int argc, char* argv[]) 
    { 
       FILE *data,*loco; 
    
        
       char buffer[1001],original_name[60],final_name[60],temporary_name[60],final_temp_name[60]; 
    
       float wavelength, signal, reference,normalized; 
    
       int i,number_of_files; 
    
    
       printf("Enter the number of files\n"); 
       scanf("%d",&number_of_files); 
    
        
       printf("original filename?\n"); 
    
        scanf("%s",&original_name); 
    
       printf(" destination filename?\n"); 
    
        scanf("%s",&final_name); 
    
        strcpy(temporary_name,original_name); 
        strcpy(final_temp_name,final_name); 
         
         
    
       for(i=1;i<=number_of_files;i++) 
       { 
    
        if(i<10){ 
        sprintf(original_name,"%s000%d.asc",temporary_name,i); 
        data = fopen(original_name,"r"); 
    
        puts(original_name); 
    
        sprintf(final_name,"%s%d.dat",final_temp_name,i); 
        loco = fopen(final_name,"w"); 
        } 
        else 
        { 
        sprintf(original_name,"%s00%d.asc",temporary_name,i); 
        data = fopen(original_name,"r"); 
    
        puts(original_name); 
    
        sprintf(final_name,"%s%d.dat",final_temp_name,i); 
        loco = fopen(final_name,"w"); 
        } 
    
    
        fgets(buffer, 1000, data); 
    
        while(feof(data) == 0) 
         { /*read some data, lets process it.*/ 
            /*extract individual values from the buffer*/ 
           sscanf(buffer, "%f%f%f", &wavelength, &signal, &reference); 
    
    
            /*calculate the normalized spectrum*/ 
           normalized = signal/reference; 
    
           /*outputs data to data file*/ 
           fprintf(loco,"%f %f\n",wavelength,normalized); 
    
            /*read the next line from the file*/ 
            fgets(buffer, 1000, data); 
    
    
          } 
           fclose(loco); 
           fclose(data); 
        } 
         
        return 0; 
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    one thing what u can do is store all your files on to one file. and later on read each file name at a time and open it.

    hope this helps

    s.s.harish

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    i mean the file name

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean something like this?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( void )
    {
        char buf[ BUFSIZ ] = {0};
        int i;
    
        for( i = 0; i < 10; i++ )
        {
            sprintf( buf, "foo%03d.bar\n", i );
            printf( buf );
        }
    
        return 0;
    }
    Now just replace foo with your random file name. Like so:
    Code:
    sprintf( buf, "%s%03d.asc", yourfilename, i );
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    quzah
    won't that just generate a filename that is filename0001.asc001.asc?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you run it and find out?

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    i've run it and it doesn't work.

    it does exactly what i said it would, unless one inputs the prefix.

    any other ideas?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I just told you you were supposed to supply the prefix. I'm not going to do the whole thing for you. Did you not read what I wrote at all?
    Now just replace foo with your random file name. Like so:
    Code:
    sprintf( buf, "%s%03d.asc", yourfilename, i );
    That means, you generate the prefix portion of the string randomly, and then in a loop, apply it the way I have suggested. It's not that hard of a concept. I can't put it any simpler here.

    1) Make a random portion in a buffer some place.
    2) Apply that to the above line of code.
    3) Be amazed that it works just like I told you it would.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    I don't want you to write me a program.

    All i need to know is how to extract the prefix from a filename that could be named anything0001.asc, where anything is really anything as per my first post explanation

    the program i have works fine if you just supply the prefix.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Um, that's what you asked for. You asked for a program that when you put in the prefix fills in the rest of the concecutively named portion. I gave you that. You supply the prefix, my program will append the suffix.

    Isn't that what your first post asks for? Sure looks like it to me.

    If you have to have them put in the full name, it's really really easy to get my version working with that. Want to know how? How long is the total suffix portion? 4 characters? 10 characters? Well whatever it is, jump that far backwards from the end of their input, and add a NULL character there. Now you've successfully chopped off the prefix. Use that prefix with what I gave you. Oh, and make sure the 3 is however many digits your numeric portion is in my code, or you'll be posting again on how it's not reading right.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    if you read the first post again, you'll see this line

    So, i'd want the program to work by inputting the datafile name completely i.e. 780100mw0001.asc.
    it looks to me as if it's the whole datafile name completely, not just the prefix.

    Perhaps it's you who should try running the given program first, to see what it does do and what it doesn't do.

    Anyway, i'll give a try to your suggestion

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 2
    Last Post: 04-22-2008, 12:07 PM
  3. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  4. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21