Command Line problems

This is a discussion on Command Line problems within the C++ Programming forums, part of the General Programming Boards category; Hello C Board, I'm a first time caller I reading thru C++ Without Fear-Overland. Okay, here's the example exercise I'm ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    Unhappy Command Line problems

    Hello C Board,
    I'm a first time caller I reading thru C++ Without Fear-Overland. Okay, here's the example exercise I'm having a problem with:

    Code:
    //the project name is readtxt2 and the text file is name output.txt 
    #include <stdafx.h>
    #include <iostream>
    #include <fstream>
    #include <string.h>
    using namespace std;
    
    int main(int argc, char *argv[]) {
        int c;   // input character
        int i;   // loop counter
        char filename[81];
        char input_line[81];
    
        if (argc > 1)
            strncpy(filename, argv[1], 80);
        else {
            cout << "Enter a file name and press ENTER: ";
            cin.getline(filename, 80);
        }
    
        ifstream file_in(filename);
    
        if (! file_in) {
            cout << "File " << filename << " could not be opened.";
            return -1;
        }
    
        while (1) {
            for (i = 1; i <= 24 && ! file_in.eof(); i++) {
                file_in.getline(input_line, 80);
                cout << input_line;
            }
            if (file_in.eof())
                break;
            cout << endl << "More? (Press 'Q' and ENTER to quit.)";
            cin.getline(input_line, 80);
            c = input_line[0];
            if (c == 'Q' || c == 'q')
                break;
        } 
        return 0;
    }
    Okay, that is the sample answer, i didn't change it.

    I am told I can enter in the command line this:
    readtxt2 output.txt
    and it should open output.txt and display for me, but when I run it and enter:
    readtxt2 output.txt
    I am told file not cannot open, now when I just enter:
    output
    it works fine. why is that?
    btw I'm using microsoft visual studio 2010 on windows vista.

    I know this is a beginner question, so please bear with me.
    I can use all the help I can get, since I am trying to learn by homeschool, thanks.
    Last edited by Salem; 03-08-2011 at 01:10 AM. Reason: Code tags around only the code

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    I would change strncpy() to strcpy(), the padding that may happen with strncpy() may cause problems. And in your if statement print out the value of argv[1] to insure the program is receiving what you entered.


    Jim

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    I would change
    Code:
        char filename[81];
        char input_line[81];
    to
    Code:
        std::string filename;
        std::string input_line;
    Code:
    strncpy(filename, argv[1], 80);
    to
    Code:
    filename = argv[1];
    Code:
            cin.getline(filename, 80);
    to
    Code:
            std::getline(std::cin, filename);
    Code:
    ifstream file_in(filename);
    to
    Code:
    ifstream file_in(filename.c_str());
    (Requirement.)

    I'll leave the rest as an exercise.
    After you made the changes, test again, and see if it works better this time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    Question Thanks guys for replying, but...

    Code:
    Thank for replying guys.  I've used both styles and they both work fine.  I may not have expressed my question clearly.  I am wondering why the book asks me to enter 2 commands to open output.txt.  (it works fine when entering just filename).
    I am instructed to enter "readtxt2" "output.txt" into the command-line when promted.
    When I enter just "output"  the programs runs fine, but when I enter 
    "readtxt2" "output" the file is not found.
    
    I've saved a txt file named output in the project folder readtxt2.  I'm reading C++ without fear by overland, I am using MS Visual Studio 2010, on a MS Windows Vista OS.
    
    So I guess what I'm asking is why will it not recognize the "readtxt2" or even the ".txt"?  Is that just the book lingo and I am not supposed to use it?  (even thought it clearly states I enter "readtxt2 output.txt" into the command-line.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 05:38 PM
  2. tile colliision detection problems
    By werdy666 in forum Game Programming
    Replies: 1
    Last Post: 10-23-2002, 09:26 PM
  3. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 01:47 AM
  4. Problems with my RPG app
    By valar_king in forum Game Programming
    Replies: 1
    Last Post: 12-15-2001, 07:07 PM
  5. problems with too many warning messages?
    By Isometric in forum C Programming
    Replies: 9
    Last Post: 11-25-2001, 12:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21