0

I posted a similar code earlier, but I think this is a different issue now. I just can't figure out why my run code won't go past "infile open". ("-e" prints out, "-d" doesn't) I'm trying to open my file, use a command line option to determine if I will print out a certain multiple of characters.

For example, a.out -d 2 < sample.txt will print out every second letter.

int main (int argc, char *argv[])
{
   ifstream infile; 

   if (infile.good())
      printf("infile open \n");

   int c;    
   int number = 0;
   int count = 0; 


   string str1 = argv[1];
   string str2 = "-d";
   string str3 = "-e";


   if (str1.compare(str2) == 0)
   { 
      printf("entered -d\n");
      c = infile.get();       

         while(!infile.eof()) {

             number = atoi(argv[2]);  

              if (count == number)
            {
              cout.put(c);
                      count = 0;
                }
                  else
                      count++;

             c = infile.get();         

}//end while 

}//end if

           if (str1.compare(str3) == 0)       
                printf("entered -e\n");


}//end main
harman2012
  • 103
  • 3
  • 8
  • You should avoid using `while (!eof())` and `atoi`. – chris Nov 29 '12 at 08:11
  • It's part of my assignment... What would be a better option? – harman2012 Nov 29 '12 at 08:14
  • `atoi` can be replaced with one of the things [here](http://stackoverflow.com/questions/5290089/how-to-convert-a-number-to-string-and-vice-versa-in-c). I tend to like `boost::lexical_cast`, though there's a little bit more error checking you can do on top of that. `while (!infile.eof())` should be `while (infile.get(c))` with the loop tailored to that. – chris Nov 29 '12 at 08:19
  • @harman2012 What do you mean, part of your assignment? The use of `infile.eof()` is not correct C++; your teacher can't be requiring it. And `atoi` is a legacy function, which shouldn't be used in modern code. – James Kanze Nov 29 '12 at 08:45

2 Answers2

4

infile is never opened:

ifstream infile; // Does not open a file as no file name is supplied.

Either use cin or pass "sample.txt" as another command-line argument and open it:

ifstream inFile(argv[3]);
if (inFile.is_open())
{
}

Other points:

  • Use std::cout instead of mixing printf() and std::cout.
  • atoi() returns 0 if the argument is invalid or if the argument is a valid 0. See strtol() for an alternative.
  • There is no reason to convert argv[2] on every iteration of the while. Just do it once prior the while.
  • Always check argc before accessing the elements of argv, to avoid invalid memory access.
  • std::string instances can be compared using operator==.
hmjd
  • 120,187
  • 20
  • 207
  • 252
0

When running something like this on command line: "a.out < sample.txt", there is no actual filename specified to open, the "<" command in Unix will just make the contents of sample.txt be passed to a.out via standard input...therefore, like hmjd pointed out, you should use cin. You would want to physically open a file if it was supplied as an argument, i.e. "a.out sample.txt" as opposed to "a.out < sample.txt".

innospark
  • 778
  • 1
  • 5
  • 8
  • Is there a reason why you would choose "a.out sample.txt" over "a.out < sample.txt" or vice versa? – harman2012 Dec 09 '12 at 04:19
  • Sorry for the late response, not sure to tell you the truth. I had to deal with this type of issue for school based on whatever specifications were given (as to how the programs would be tested by the TA's). But if I'd have to think about it, if you use "a.out sample.txt" I would assume that you can do more rigorous checks on the validity of the input from within the program (seeing as it was passed in as an argument), whereas with a pipe you can pretty much pipe anything to that program and will have a hard time discriminating against valid/invalid input, this is what I would assume. – innospark Jan 09 '13 at 08:28