Input / Output - How to Create File?

This is a discussion on Input / Output - How to Create File? within the C++ Programming forums, part of the General Programming Boards category; Hey, Currently I making a little program that takes: Clients name and then the tracking code for that client and ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    22

    Thumbs up Input / Output - How to Create File?

    Hey, Currently I making a little program that takes: Clients name and then the tracking code for that client and saves it in a 'char array'

    However, my following code is not producing a file:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string.h>
    
    using namespace std;
    
    
    int main()
    {
        ofstream firstClass;
        char cname[25];
        char trackno[14];
        char outFileName[256];
        int z;
    
    
        cout <<"Enter the client name: "; cin >> cname;
    
    
        cout <<"Okay, Thats saved..." << endl; 
        cout <<" " << endl;
    
    
        cout <<"Please now enter the tracking code: "; cin >> trackno;
    
    
        cout << "Set the output file name: "; cin.getline(outFileName,256);
    
    
        firstClass.open(outFileName);
        firstClass << trackno;
        firstClass.close();
    
    
        cin >> z;
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    I suggest that you:
    • Declare variables near first use.
    • Keep each statement on its own line.
    • Tell us how exactly does your program not work. (What exactly do you mean by "not producing a file"? Any error messages? What is the input, expected output and actual output?)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    This one of the problems when mixing numeric input and character input. When you retrieve the trackno variable you leave the end of line character in the input buffer which the cin.getline() retrieves as the input to it's variable, skipping any actual entry of the name. To fix the problem you need to retrieve that end of line character. Use either a cin.get() or cin.ignore() to retrieve the problem characters.

    Also you should always place only one command per line.
    Code:
    cout <<"Please now enter the tracking code: "; cin >> trackno;  // This is a bad practice IMO.
    // This is better.
    cout <<"Please now enter the tracking code: ";
    cin >> trackno;
    This will make debugging much easier in the future and also be easier not to miss the input.

    Jim

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    22

    Thumbs up

    Here's the new code file

    Code:
    #include <iostream>#include <fstream>
    #include <string.h>
     
    using namespace std;
     
     
    int main()
    {
        ofstream firstClass;
     
        char cname[25]; // Char array that holds the client name, ie; Ian Bell;
        cout <<"Enter the client name: "; // Prompting user to enter client name
        cin >> cname; // Takes the name of the client
     
        cout <<"Okay, Thats saved..." << endl; // Displaying friendly message to aknowledge user that Client name was taken successfully
        cout <<" " << endl;
     
        char trackno[14];
        cout <<"Please now enter the tracking code: "; // Here it asks the user to enter the tracking code (Shipping code)
        cin >> trackno; // Takes the tracking code
     
        char outFileName; // Variable that stores the filename user wants
        cout << "Set the output file name: "; 
        outFileName=cin.get(); // 
    
    
    
        return 0;
    }

    @ laserlight

    I am trying to basically create 'new file' that writes the client name in that file and the tracking code. What lines would I add after getting the filename from user.

    Sorry Very Noob Programmer and Hell slow learner..
    Last edited by Swadesh; 07-11-2012 at 01:29 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Where are you getting the file name from the user? The following only gets one character:
    Code:
    char outFileName; // Variable that stores the filename user wants
        cout << "Set the output file name: ";
        outFileName=cin.get(); //
    Jim

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    22

    Thumbs up

    Quote Originally Posted by jimblumberg View Post
    Where are you getting the file name from the user? The following only gets one character:
    Code:
    char outFileName; // Variable that stores the filename user wants
        cout << "Set the output file name: ";
        outFileName=cin.get(); //
    Jim
    Hey, actually I was following what you said..

    Is this legal:

    Code:
    char outFileName[10]; // Variable that stores the filename user wants   
    cout << "Set the output file name: "; 
        outFileName[10]=cin.get(); //
    It is giving following error but doesnt show any compile errors
    Stack around the variable 'outFileName' was corrupted.


  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Hey, actually I was following what you said..
    No, I said you need to retrieve and dispose of the end of line character before you try to retrieve your string. Not try to retrieve a string in a single character.

    Is this legal:
    I think the following answers that question, don't you?
    It is giving following error
    Go back to your original program and before you try to retrieve your string with getline() try placing a cin.get(). And also note that cin.get() gets a single character, not a string.

    Jim

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,025
    I strongly suggest you:
    * Use std::string instead of char arrays.
    * Use std::getline to read a line.
    What you are doing in unsafe and can result in buffer overflows. Don't do it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ File Input Output VS C file Input Output
    By forumuser in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2009, 07:46 AM
  2. Input-Output File--Can't create a file...
    By zaracattle in forum C++ Programming
    Replies: 10
    Last Post: 10-18-2006, 11:15 AM
  3. file input/output
    By Bigbio2002 in forum C Programming
    Replies: 3
    Last Post: 10-29-2003, 01:45 AM
  4. file input and output
    By isaac in forum C Programming
    Replies: 3
    Last Post: 06-04-2002, 05:41 PM
  5. File Input-output
    By ucme_me in forum C Programming
    Replies: 6
    Last Post: 12-14-2001, 02:27 PM

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