GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-May-2009, 07:25
nanchuangyeyu nanchuangyeyu is offline
New Member
 
Join Date: Apr 2009
Posts: 15
nanchuangyeyu has a little shameless behaviour in the past
Question

Questions about handling a batch of files.


Hello,
I am trying to handle(read,write and so on) a batch of files named regularly as "name1,name2,name3..."at once. Any body can tell me how to efficiently pass the file name parameter to the handling functions like .read() and .write()? It is a very common question and I think there should be some "formal" solutions.Thank u in advance.
nanchuangyeyu
  #2  
Old 01-May-2009, 08:06
mathmate mathmate is offline
New Member
 
Join Date: Apr 2009
Posts: 7
mathmate will become famous soon enough

Re: Questions about handling a batch of files.


I am not sure if I understand your question correctly. Let me know if I didn't.
Depending on the regularity of your usage, you may want to generate the file names on the fly in the program, or if the file names are subject to updates, you can put them in a file and subsequently read them into an array of strings.
You can then do your routine fopen() or whatever is needed to open the file using each member of the file name array.
  #3  
Old 01-May-2009, 08:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Questions about handling a batch of files.


Quote:
Originally Posted by nanchuangyeyu
Hello,
...files named regularly as "name1,name2,name3..."at once. Any body can tell me how to efficiently pass the file name parameter to the handling functions like .read() and .write()?
1. You don't pass a file name to .read() or .write(). Those are member functions of a stream class, and are used on a stream object after a file has been opened. The file name is used when the program opens a file. It can be opened when it the object is created, or it can be opened with the stream member function .open().

2. You can see an example of one way to make integers in a loop be part of a file name in this recent thread: http://www.gidforums.com/t-21148.html

Quote:
Originally Posted by nanchuangyeyu
...I think there should be some "formal" solutions...

Well, maybe life would be simpler if there were only one way to do each and every task that is assigned to us, but in C++ there are about a million ways (maybe more) to do most things.

Some people use old-fashioned C program approaches that combine integers and "strings" in arrays of chars. The C standard library function sprintf() can do this. Some people use non-standard functions like itoa() to do this (assuming that their compiler libraries contain such a function).

I personally think that people learning C++ are best served by learning to take advantage of "C++" ways, and in particular, I consider gratuitous use of char arrays to hold C-style "strings" in 21st century C++ programs to be "so 20th century." But that's just me. I'm funny that way. See Footnote.


Regards,

Dave

Footnote: Note that I said gratuitous use. There certainly are situations where one might make a argument that use of arrays of chars holding C-style "strings" is appropriate.

As far as your comment about "formal" methods to accomplish a particular task, Consider the famous quote from Ralph Waldo Emerson: "A foolish consistency is the hobgoblin of little minds."

My feelings are that, in general, consistency is a Good Thing in programming, for a lot of reasons, but insistence on a single set way to perform a particular task may not be the most effective way to design applications.

Note the modifier "foolish" in the Emerson quote. The big question (to me, at least) is: Who gets to decide what is "foolish"? My pragmatic answer: For most people the person who assigns your grade or who signs your paycheck. That's who. Not some wordy pedant (whom you don't even know) who occasionally spouts off on a public forum on the internet.
Last edited by davekw7x : 01-May-2009 at 09:51.
  #4  
Old 01-May-2009, 10:54
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Questions about handling a batch of files.


Yes , I learned a C++ thing today!
Like dave said If we're in C++ we should do C++.
Here are two ways. First I practiced a C style , then I learnt a C++ way.
CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

/* C++ string style */
int main(void)
{
  string Base_Name = "workdat_"  ,  fname;
  stringstream oss;
  int i;

  for(i=0; i < 4; i++)
  {
    //fname = Base_Name + i;  i won't convert

    //   Could use C style itoa() but Stroustrup tells us USE C++ 
    //   so write a formatted string to a sstream
    oss << Base_Name << i;
    fname = oss.str();           // there, was that so hard????

    cout << fname << endl;
    oss.seekp(ios_base::beg);  //set pointer to beginning  (comment out for fun)

    fstream fp( fname.c_str(),  fstream::out); 
    if(fp.good())
    {  
      // write stuff
      fp.close();
    }
    else
      cout << "uhoh" << endl; // make sure to to add error checks
  }
  return 0;
}

/****  C_string - sprintf() style  ****
 
int main(void)
{
  char Base_Name[] = "workdat_", fname[64] = {0};
  int i;
  
  for(i=0; i < 4; i++)
  {
    sprintf(fname, "%s%d", Base_Name, i);

    fstream fp(fname,  fstream::out);
    
    fp.close();
  }
  return 0;
}
*/
Watch where you aim that thing so you don't overwrite good files inadverently!
  #5  
Old 01-May-2009, 13:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Questions about handling a batch of files.


Quote:
Originally Posted by Howard_L
...dave said If we're in C++ we should do C++.
Howard: I usually don't get personal here, but I have to say this: I love you like a brother, but please don't put words in my mouth.

I gave an opinion that I personally feel that people learning C++ are "best served" by doing certain things. I didn't take it on myself to say what people "should" do. See Footnote.

As in this case we typically have no way of knowing whether Posters are constrained by some specific assignment requirements, and sometimes our advice doesn't really apply to their particular situation. I mean, we try, but...


Regards,

Dave

Footnote: Sometimes I feel really strongly about what they should not do, however, and I try to give the reasons for my objections, rather than just scream that they shouldn't "do that." (You know: "DON'T USE GOTO!" "DON'T USE GETS()." "DON'T EAT YELLOW SNOW!" Stuff like that.)

On the other hand, this seems to be my "Emerson" day, so I will mention that he also said, "Sometimes a scream is better than a thesis." I'm guessing that his intent was sardonic, but then again...
  #6  
Old 01-May-2009, 14:15
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Questions about handling a batch of files.


Quote:
please don't put words in my mouth.
You're right, sorry.
I just wanted to acknowledge that I had seen your post and agreed with what you had said.
I will do better. (note to self.. do better...)
Last edited by Howard_L : 01-May-2009 at 15:11.
  #7  
Old 01-May-2009, 20:37
nanchuangyeyu nanchuangyeyu is offline
New Member
 
Join Date: Apr 2009
Posts: 15
nanchuangyeyu has a little shameless behaviour in the past

Re: Questions about handling a batch of files.


Quote:
Originally Posted by davekw7x
As far as your comment about "formal" methods to accomplish a particular task, Consider the famous quote from Ralph Waldo Emerson: "A foolish consistency is the hobgoblin of little minds."
Thank you dave, I am a green hand in C++ and your guide is really helpful to me. Surely "foolish consistency" results from the lack of thorough knowledge of C++. Having only one option at hand will inevitablly result in "foolish consistency" . Any way,thank you again for your guide.I will go on to learn more about C++.
nanchuangyeyu
 
 

Recent GIDBlogProblems with the Navy (Chiefs) 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
jGrasp Compile Error cardude C++ Forum 4 29-Sep-2009 21:48
Handling avi files in C/C++...urgent...please!!! karunr MS Visual C++ / MFC Forum 0 14-Feb-2008 03:21
Bloodshed Dev C++ Project Options JdS C++ Forum 6 11-Nov-2005 18:23
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 03:26
How to create batch files without DOS box? rhino1616 Web Design Forum 3 18-Aug-2003 11:00

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

All times are GMT -6. The time now is 16:39.


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