Thread: Creating several(400) data files within a program

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    26

    Creating several(400) data files within a program

    I'm very new in c programming, so my quetion may seem fooly.

    I write a program that calls gnuplot; it have to solve 200 ordinary diferential equiations. My question is: How I rename the images and the data files, so my program stores all the solutions and its graphics?

    Thanks a lot.

  2. #2
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    you're going to need some practice, look up file streams...

    the algorithm would work as such:
    create a new file with preferred names,
    copy data from one file to another,
    delete old files.

    http://www.cprogramming.com/tutorial/cfileio.html

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    oh... lol, oops... :-) well now i know.

    i guess now that i think about it, what i said is extremely impractical...

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    26
    But rename doesn't work. At last I don't see how.

    My problem is this:

    1. I solve an numerical integration and I save the data in a file, "file1.dat"
    2. I call Gnuplot, and gnuplot uses "file1.dat" to create an image file, that I call "file1.eps"
    3. I change the initial conditions of the equition and repeat the procces.

    Now, If I use rename, it will be like this.

    rename("file1.dat","file2.dat")

    But what happen if I make another integration? ¿It isn't posible in C to do something like file[].dat, where [] is a variable which changes when a variable increses?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Instead of renaming the file, why not create it in a uniquely named file in the first place?

    You could create a file-name using sprintf() for example...

    --
    Mats

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    26
    I'm not sure I'm understanding you.

    You say, put all the text in the same file? That's ok. And that work's fine without additional functions. But I still have the problem with the images files.

    I've seen sprintf function, but I don't understand the idea very well.

    If I say gnuplot to create an image file the first time, it's going to be whatever i say. The second time, sprintf will have create a file - if a do something like sprintf(string, "file. %d", file_number) -, but how I tell gnuplot to create the images in that file?

    Sorry very much, but i have dificulties with english and with programming ideas, so i fell i don't get it the faster i have to.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Just stuff all of the data into one binary file. Why create 200 files?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    but how I tell gnuplot to create the images in that file?
    Do you mean, how can you redirect gnuplot's output into a different file?
    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.

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    26
    Ok. The first problem is solved (I save of the data in an unique file.dat).

    The second problem is just what dwks say: ¿how can i save the gnuplot's output in different files ITERATIVELY? I need the 200 output images files!

    I'm searching information about binary files (i'm new in c programming -in general in programming -)

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Okay . . . so you want to generate a filename for gnuplot to save to, correct? Then you can either pass that directly to gnuplot for its output file, or rename gnuplot's output file to this new name.

    Well, I think sprintf() would work well here, as you mentioned earlier.
    Code:
    sprintf(filename, "file-%d.output", number);
    Then, when you execute gnuplot, use filename. If you're using system, consider something like (I don't know gnuplot's arguments):
    Code:
    sprintf(command, "gnuplot %s -o %s", input, output);
    system(command);
    With exec*(), just pass output as another argument.

    You'll have to be more specific. How are you calling gnuplot?
    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.

  12. #12
    Registered User
    Join Date
    Aug 2007
    Posts
    26
    Perfect! I solved the problem! Actually I got more problems if a don't create the multiple data files, so I did this:

    Code:
    FILE     *fp[101]; //number of files I need
    char     fname[25];
    
      for ( k = 0; k < 101; k++){
        sprintf(fname,"ros&#37;d.dat", k);
        fp[k] = fopen(fname, "w");
      }
    And at the end I do...

    Code:
    for ( i = 0; i < 101; i++) fp(fout[i]);
    Thanks a lot for your sugestions.

    About Gnuplot, I was calling it like this:

    Code:
    #define GNUPLOT_PATH "/usr/local/bin"
    At the begining it worked, but then gnuplot don't show me something, or stored any graphics, so i made a little script which call the data files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing data in two files
    By nynicue in forum C Programming
    Replies: 25
    Last Post: 06-18-2009, 07:35 PM
  2. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 11:21 AM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. Replies: 4
    Last Post: 06-14-2005, 05:45 AM