GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Jul-2005, 19:53
Peter11111 Peter11111 is offline
New Member
 
Join Date: Jul 2005
Posts: 16
Peter11111 is on a distinguished road

Using fopen on multiple files


I have about 100 csv files that I want to open. Each csv file containts similar data, and I need to analyse the first line of each csv file. The problem is how do I loop around fopen. Each csv file has incrememnting file numbers, so file1, file2, file3 ... The obvious solution is call fopen 100 separate times, but that is just being stupid.
  #2  
Old 27-Jul-2005, 20:27
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 133
L7Sqr has a spectacular aura aboutL7Sqr has a spectacular aura about
I am assuming you want to know how to set up the file name for the files....
If that is the case, and the files are as you suggest...

CPP / C++ / C Code:
char * file_pat = "file%d";
char file[BUF];
int i = 1;
for (; i <= 100; +i) {
   snprintf (file, sizeof (file), file_pat, i);
   ...
   do what you want
   ...
}

Just replace file_pat to fit your actual pattern (or use your
pattern directly in the snprintf statement)

If you are talking about files that may or may not be in order (some numbers may be missing) and/or the number of files isnt know, you can use opendir and sscanf. If that is what you want post back and I'll give an example...
  #3  
Old 27-Jul-2005, 20:47
Peter11111 Peter11111 is offline
New Member
 
Join Date: Jul 2005
Posts: 16
Peter11111 is on a distinguished road
I think the best way to explain what I have done is by showing you:
This is what I have so far, and seems to work fine

CPP / C++ / C Code:
for(i=1;i<101;i++) 
{
 fp = fopen(argv[i], "r");  
 if ((fp) == NULL)
   fprintf(stderr, "Cannot open %s\n", argv[1]);
 else
   fgets(string , 100 , fp);
   ....

As you can see the problem is having 100 arguments on the command line.
Last edited by LuciWiz : 28-Jul-2005 at 02:22. Reason: Please insert your C code between [c] & [/c] tags
  #4  
Old 28-Jul-2005, 05:10
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 133
L7Sqr has a spectacular aura aboutL7Sqr has a spectacular aura about
So have the program take a format string as the argument.
for instance:

./program file_name_%03d.csv

Or some such that will match the pattern of your file.
  #5  
Old 29-Jul-2005, 21:42
Peter11111 Peter11111 is offline
New Member
 
Join Date: Jul 2005
Posts: 16
Peter11111 is on a distinguished road
Quote:
Originally Posted by L7Sqr
So have the program take a format string as the argument.
for instance:

./program file_name_%03d.csv

Or some such that will match the pattern of your file.

Have tried what you mentioned, but cannot seem to get it in the correct format
The file names are form1.cvs, form2.cvs ...
  #6  
Old 29-Jul-2005, 22:05
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by Peter11111
Have tried what you mentioned, but cannot seem to get it in the correct format
Expected input/output? Code sample (real, not "summarized")?
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple file creation with fopen? robin C Programming Language 6 16-Aug-2006 23:38
Linker errors with multiple file progam nkhambal C Programming Language 2 24-Apr-2005 02:37
controling dialog objects & multiple source files omills MS Visual C++ / MFC Forum 0 14-Jul-2004 23:30
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:57.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.