Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8925577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:44:08+00:00 2026-06-15T07:44:08+00:00

I posted a similar code earlier, but I think this is a different issue

  • 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:44:09+00:00Added an answer on June 15, 2026 at 7:44 am

    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==.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've posted a similar topic/question, but this time it's a bit different. I'm just
I have posted similar question previously, but this time I am providing some code
I know that I was already posted similar question but I just can't find
I know there have been similar questions posted but I think the issue I'm
I posted another question similar. This is my old code as some ppl say
I posted a similar question earlier, but it was deleted because I felt I
[I just posted a similar question, but I'm not sure whether is actually got
I posted a similar RBL question but I have a new issue arising so
Someone has posted a similar question to this with no resolution, but also no
I posted another question on a very similar topic, but turned out to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.