Thread: newbie question about i/o!!!!!!!!help!!!!11

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    newbie question about i/o!!!!!!!!help!!!!11

    Code:
    //Listing 17.16 Opening Files for Read and Write
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char fileName[80];
    	char buffer[255];    // for user input
     cout << "File name: ";
    	cin >> fileName;
    
    	ofstream fout(fileName);  // open for writing
    	fout << "This line written directly to the file...\n";
    	cout << "Enter text for the file: ";
    	cin.ignore(1,'\n');  // eat the newline after the file name
    	cin.getline(buffer,255);  // get the user's input
    	fout << buffer << "\n";   // and write it to the file
    	fout.close();             // close the file, ready for reopen
    
    	ifstream fin(fileName);    // reopen for reading
    	cout << "Here's the contents of the file:\n";
    	char ch;
    	while (fin.get(ch))
    		cout << ch;
    
    	cout << "\n***End of file contents.***\n";
    
    	fin.close();            // always pays to be tidy
    	return 0;
    }

    I thought this code would open like a window or something and save whatever is in there to a file but it doesn't work!!!! What is wrong with this? I am just learning about that right now so I know this might sound stupid.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    ifstream is input file stream (or at least that's how I remember it).

    You are using cout so it will output into the same window. Basically, all the program will do is read the file and print its contents in the console.

    To copy it, you would have to save all of the char's into a buffer and create an ofstream and write into the file w/ the ofstream (Output File Stream or that's how I remember it).

  3. #3
    Unregistered
    Guest
    #include<iostream>
    #include<fstream>
    #include<string>
    using namespace std;

    int main()
    {
    ifstream ifile;
    ifile.open("input.txt");

  4. #4
    Unregistered
    Guest
    #include<iostream>
    #include<fstream>
    using namespace std;

    int main()
    {
    ifstream ifile; //for input
    ofstream ofile; //for output

    //now open the files

    ifile.open("input.txt");
    ofile.open("output.txt");
    if(!ifile)
    {
    cout << "Could not open file!");
    return 1;
    }

    //read from the input file

    while(!ifile.eof() ) { //while not end of file
    //get the input
    ofile << "output to the file";

    }

    ifile.close();
    ofile.close();
    }

  5. #5
    Unregistered
    Guest
    By the way ... that's not a working program .. .just demostrates how to open, write to and close a file .... ... ...


    if question's ... please e mail me @ [email protected]

  6. #6
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I see what you mean thanks......I am just learning this right now........so this is kind of hard for me.....Mr. Jesse Liberty does a really poor job at explaining this.......seriously........
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  7. #7
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Ok this is driving me nuts......how would you get this code to work
    Code:
    //Listing 17.16 Opening Files for Read and Write
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char fileName[80];
    	char buffer[255];    // for user input
     cout << "File name: ";
    	cin >> fileName;
    
    	ofstream fout(fileName);  // open for writing
    	fout << "This line written directly to the file...\n";
    	cout << "Enter text for the file: ";
    	cin.ignore(1,'\n');  // eat the newline after the file name
    	cin.getline(buffer,255);  // get the user's input
    	fout << buffer << "\n";   // and write it to the file
    	fout.close();             // close the file, ready for reopen
    
    	ifstream fin(fileName);    // reopen for reading
    	cout << "Here's the contents of the file:\n";
    	char ch;
    	while (fin.get(ch))
    		cout << ch;
    
    	cout << "\n***End of file contents.***\n";
    
    	fin.close();            // always pays to be tidy
    	return 0;
    }

    You know like actually opening a file and saving whatever it's in there.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  8. #8
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Sometimes I hate Windows so freaking much.....Turn out the problem I was having was because I had to restart windows.......you know how windows sometimes stops responding to some commands? Yes that's what happened....so it wouldn't write the file because Windows was *****ing. If I had had a heart attack and died it would've have been because of microsoft. So anyways the way I found out is that I tried to open a window on yahoo's instant messenger and it wouldn't open so I said hey why not restart........................................... ..........
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  4. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM