Need help opening a series of files

This is a discussion on Need help opening a series of files within the C Programming forums, part of the General Programming Boards category; So I'm writing a function in my program which will, among other things, open a bunch of files and read ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    25

    Need help opening a series of files

    So I'm writing a function in my program which will, among other things, open a bunch of files and read data from them through a for loop. The file names are in the format "Lightcurves\modelB.##.mag" where ## refers to i, the variable I'm looping through, which goes from 0 to 90 in steps of 5.

    Before I put in the loop with the fscanf, it worked fine and said it opened all the files properly, now it crashes when I run it. I'm not quite sure how to read the data now because that hasn't ever happened to me before.

    After a bit of help with a particular problem in another thread, I've got this now:

    Code:
    FILE *model; //All the global declarations that I use in fitCurves(), I think.
    char *modelname[];
    char *mode = "r";
    
    void fitCurves()
    {
         int i; //Inclination.
         int modelsize; //Number of data points in model.
         float phase[400]; //Phase of model.
         float expmag[400]; //Expected magnitude of model.
         char inc[3]; //Temporary string.
         char *modelfile[19][20]; //Model file name.
         char *temp; //Temporary string.
         for (i = 0; i <= 90; i += 5) {
             sprintf(inc, "%02d", i);
             temp = malloc(strlen(modelname) + strlen(inc) + strlen("Lightcurves\\") + strlen(".mag") + 1);
             sprintf(temp, "%s%s%s%s", "Lightcurves\\", modelname, inc, ".mag");
             strcpy(modelfile[i/5], temp);
             free(temp);
         }
         for (i = 10; i <= 90; i += 5) { //Loops through all inclinations.
             model = openFile(modelfile[i/5]);
             modelsize = 0;
             while (!feof(model)) {
                   fscanf(model, "%f %f %f", &phase[modelsize], &expmag[modelsize]); //Scans file and reads data.
                   modelsize++;
             }
             modelsize--; //Readjusts size variable to account for final size++.
             fclose(model);
         } //Ends loop.
    }
    
    FILE* openFile(char *name)
    {
         FILE* file;
         file = fopen(name, mode); //Open input file.
         if (file == NULL) { //Error handling.
                fprintf(stderr, "Can't open input file %s!\n", name);
                //system("pause");
                //exit(1);
         } else {
                printf("File %s opened successfully.\n", name);
         }
         return file;
    }
    Thanks so much for any help!!

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    EDIT: I feel kind of dumb, I just forgot to assign the third float in fscanf to a variable....it all works well now. How do I delete this?

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    I tried that, I can't see where it lets you delete.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Yeah, I only have save, go advanced, and cancel (just for the first post)......too late, I guess?

  5. #5
     is offline
    ex-samurai 's Avatar
    Join Date
    Nov 2006
    Location
    England
    Posts
    18
    LOL! quadraple posts! testing testing...first post.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Nice first post . . . and nice name.

    Besides, it looks like someone else posted something and then deleted it.

    Code:
    while (!feof(model)) {
    Don't do this -- see the FAQ. http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    haha...not quite my first post, glad you like the name, and thanks for the code help

    Now to just delete this thread...

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by ramparts
    haha...not quite my first post, glad you like the name, and thanks for the code help
    I wasn't talking about you, but rather the other poster. With the first post and the funny name. [edit] &#167;&#225;&#223;&#248;†&#230;™ [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Yeah, It was me... deleted my coments because thought that maybe they prevent from deleting the post...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    fscanf(model, "%f %f %f", &phase[modelsize], &expmag[modelsize])
    you read three floats but assign it to only two value holders.

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opening files.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-25-2008, 01:05 PM
  2. Opening ASCII files in C?
    By Mavix in forum C Programming
    Replies: 6
    Last Post: 04-25-2007, 02:23 PM
  3. Opening files with UNICODE file names
    By decohk in forum Linux Programming
    Replies: 2
    Last Post: 11-09-2006, 04:25 AM
  4. opening files
    By angelic79 in forum C Programming
    Replies: 3
    Last Post: 10-19-2004, 06:52 AM
  5. Opening files - Giving options?
    By wwwGazUKcom in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 07:06 AM

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