473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem opening multiple files through command line

I'll skip the nitty gritty, but here's the outline of the program

Expand|Select|Wrap|Line Numbers
  1. int main( int Argc, char *Arg[])
  2. {
  3.   ifstream indata;
  4.  
  5.   cout << "Command line contained " << Argc << " tokens\n\n";
  6.   cout << "Name of executing file: " << Arg[0] << "\n";
  7.  
  8.   for (int I=1; I<Argc; I++)
  9.     {
  10.       cout << "Attempting to open " << Arg[i] << "..." << endl;
  11.  
  12.       indata.open(Arg[i]);
  13.  
  14.       if ( !indata )
  15.       {
  16.         cerr << "Error: File could not be opened"
  17.       }
  18.  
  19.       else
  20.       {
  21.         (REST OF PROGRAM)
  22.       }
  23.     }
  24. }
  25.  
So when I run my code using only one file (test.txt, for example) the program works fine and does exactly what it needs to do.

However, if multiple files are written in, say with a dummy file B
a.out B test.txt

B shows an invalid file...but so does test.txt, which we already know works.

Also, something with two valid files
a.out test.txt test1.txt

this will simply run the first file no problem, but when it gets to the second file (simply a copy of test.txt) it reports that it's unable to open

Is there something wrong with closing the files? I have close statements in the bulk section of the program

Help is greatly appreciated,
-Myxamatosis
Apr 10 '08 #1
4 1714
mac11
256 100+
if you are sure indata.close() is being called you might want to call indata.clear() before trying to open the next file - this will clear any error status bits which may have been set when you tried to open a file that didn't exist.

http://www.cplusplus.com/reference/iostream/ios/clear.html

You may also want to consider adding some more error checking (but maybe you already have that and didn't include it in your stripped down example).
Apr 10 '08 #2
indata.close() is definitely being closed, i've used a cout statement after the close statement, and it prints fine.

Even with adding clear() I still seem to be getting the same problem. I've also changed (!indata) to (indata.fail())
Apr 10 '08 #3
okay,

So I moved the statements around a bit, and the program is now able to open multiple file commands that are valid . However, I'm back to the same problem when an invalid file is thrown in the mix

a.out test.txt B test1.txt
or
a.out B test.txt

anything after the invalid file is still regarded as invalid.

if you need to see the rest of my code (it's lengthy) just ask.
Apr 10 '08 #4
problem fixed. thanks again for the help
Apr 10 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Ken Allen | last post by:
I have been developing a suite of assemblies over the past couple of weeks, and this afternoon somethign started misbehaving. If I do not run the IDE and compiler the code from the command line,...
0
by: P Pulkkinen | last post by:
Dear all, sorry, i know this code is far little too long to debug here, but there is really annoying logical error. If someone debugs this, I really offer warm virtual handshake. What this...
1
by: alik | last post by:
I am having problem with understanding the project's concept and what exactly we need to follow. I don't know if we have to create files and how many. This is my first programing experience and I am...
10
by: kimiraikkonen | last post by:
Hi, I have an app which has a listbox and when i double click an associated fileS, i want their paths to be added into listbox in my application. This code works good when i try to open a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.