Just a simple thing.

This is a discussion on Just a simple thing. within the C++ Programming forums, part of the General Programming Boards category; Hiyaz all, i'ma little knew too this, all i need help with is... How could i open a file? just ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    7

    Just a simple thing.

    Hiyaz all,
    i'ma little knew too this, all i need help with is...
    How could i open a file? just say i wanted to do something simple.....
    Code:
    int main(int argc, char *argv[])
    {
     char yesno;
     
     cout<<"open, Y/N?"<<endl;
     cin>>yesno;
     if (yesno =='y')
     {
        <need help with this>
     }
    else
    .....................
    .....................
    if anyone could tell me what i need to use.. i would be greatful

    Thx

  2. #2
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Code:
    int main(int argc, char *argv[])
    {
     char yesno;
     
     cout<<"open, Y/N?"<<endl;
     cin>>yesno;
     if (yesno =='y')
     {
        ifstream file(argv[1]); /*creates an input stream opeing file name passed as argument*/
     }
    else
    Gheck out the tutorials, dude. I think I just read this there.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    32
    And or something like..

    Code:
    int main(int argc, char *argv[])
    {
     ifstream inputFile;
     string fileName;
     char yesno;
     
     cout<<"open, Y/N?"<<endl;
     cin>>yesno;
     if (yesno =='y')
     {
            cout << "Enter file name:";
    	cin >> fileName;
    
        	inputFile.open(fileName.c_str());
    
            // then do what you have to with it... ie:
    
           while(!inputFile.eof())   
          {
              .....
           }
    
     }
    else

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> while(!inputFile.eof())

    Bad advice there. Using eof() to control the loop usually leads small errors at the end of input. The result of operator>> or getline is a better way to control the loop.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    Would it be possible to specify a certain target?
    "C:/test.txt"?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Yes:
    Code:
    ifstream file("C:/test.txt");
    or
    Code:
    ifstream file("C:\\test.txt");

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    hrmmm
    got error on
    Code:
    ifstream file("C:/test.txt");
    saying ...
    variable 'std.ifstream file' has initializer but incomplete.#
    i'm mising something obviously

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Did you #include <fstream>?

    Post your entire code (include #includes) if you still get the error.

  9. #9
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Your slash is the wrong way, I think. C:\test.txt

    Quote Originally Posted by steveiwonder
    hrmmm
    got error on
    Code:
    ifstream file("C:/test.txt");
    saying ...
    variable 'std.ifstream file' has initializer but incomplete.#
    i'm mising something obviously

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    ok , here it all is... but first i will explain what it is meant to do.i will put it in startup when does, and i just want it to either run a prgram Teamspeak server and not KPF.... or run KPF and not Teamspeak.

    my problem is now... when you put in more than 1 letter it outputs the error message accordingly to the amount of characters enterd... i tried to change that when i added the [1] in .... but that just makes it worse, if you enter more than 1 charcter it completly spams the error message.

    also, it wont open the "C:/test.txt"

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        char yn[1];
        cout<<"Do you wish to run TS server?.... enter Y or N\n"<<endl;
        cin>>yn[1];
        yn[1] = tolower (yn[1]);
        if (yn[1] != 'y' && yn[1] != 'n')
        {
           do
           {
               cout<<"Error!, option was invalid, pleaee enetr Y or N\n"<<endl;
               cout<<"Do you wish to run TS server?.... enter Y or N\n"<<endl;
               
               cin.getline(yn, 1);
               yn[1] = tolower (yn[1]);
            }while (yn[1] != 'y' && yn[1] != 'n');
         }
         if (yn[1] == 'y')
        {
               
             cout<<"You chose too run TS server and not KPF"<<endl;
             ifstream file("C:\\test.txt");
            
        }
        else if (yn[1] != 'y' && yn[1] == 'n')
        {
             cout<<"\nYou chose not to run TS and run KPF"<<endl;
             
       
        }
       system("PAUSE");
       return EXIT_SUCCESS;
    }

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    switching to getline is a good way to prevent the looping problem you saw, but you introduced many problems.

    For this, I think you can still use cin, just flush the buffer if you encounter bad data.
    Code:
        char choice;
        int error = 0;
    
        do
        {
            if(error) {
                cout << "Error!, option was invalid, please enter Y or N" << endl;
            }
    
            cout << "Do you wish to run TS server?.... enter Y or N" << endl;
            cin >> choice;
    
            choice = tolower(choice);
    
            if(choice != 'y' && choice != 'n') {
                error = 1;
                //bad user entry... flush buffer (from FAQ)
                cin.clear();
                cin.ignore(std::numeric_limits < int >::max(), '\n');
            }else{
                error = 0;
            }
        }while (error);
    
        if (choice == 'y') {
            cout << "You chose to run TS server and not KPF" << endl;
        } else {
            cout << "You chose not to run TS and run KPF" << endl;
        }

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    thanks that seem to work, any1 know how i could fix the problem of it not opening the .txt file?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I think there is a misunderstanding here. In progamming, when you open a file, you open it so that you code can look at the contents or add new data to it. It sounds like maybe you want the file to open in Notepad or something? If that is the case, then forget the fstream stuff, you need ShellExecute() or system() or something like that. Otherwise, your code never checks to see whether the fstream open worked.

    Also, your original yesno stuff was correct, but your latest yn stuff is wrong. You create an array with one element, but then use index 1 which is illegal for a 1 element array (indexes are 0 based). Switch the yesno stuff back to how you had it originally.

    The solution to the error part is not getline, but rather ignore(). You need to ignore the extra characters if the input is bad. cin.ignore(1000, '\n') will ignore up to 1000 extra characters.

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    yup, thats exactly what i want it too do.... could you tell me how?

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Lookup ShellExecute() or system(), (or something else). The solution depends on your OS. I believe there is a FAQ about starting another program from within yours, look there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM
  2. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM
  3. Help me with these simple programs
    By Help me in forum C Programming
    Replies: 4
    Last Post: 11-08-2001, 09:38 AM
  4. Need help with simple data types
    By partnole in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2001, 08:36 AM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM

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