#include <iostream> #include <fstream> #include <string> int main() { using namespace std; string input1[4] = { }; cout << "Enter 4 lines separated by enter: "; cin >> input1[4]; // create output stream object for new file call fout ofstream fout(input1[4].c_str()); fout << "Here are the lines\n"; // open file for writing ifstream input(input1[4].c_str()); cout << "Here are the contents of " << input << ";\n"; char ch; while (input.get(ch)) cout << ch; cout << "Done\n"; input.close(); return 0; }
19 Replies - 6051 Views - Last Post: 07 August 2012 - 11:52 PM
#1
Opening text files for input
Posted 07 August 2012 - 04:19 PM
Replies To: Opening text files for input
#2
Re: Opening text files for input
Posted 07 August 2012 - 04:35 PM
Line 17 should be crashing for you, or at worse quitely running and corrupting memory because you are accessing the 5th element of a 4 element array.
#3
Re: Opening text files for input
Posted 07 August 2012 - 04:41 PM
Skydiver, on 07 August 2012 - 04:35 PM, said:
Line 17 should be crashing for you, or at worse quitely running and corrupting memory because you are accessing the 5th element of a 4 element array.
Ah yes you are correct. But is my method for opening up the text file for inputing 4 lines correct? And output string? Thank you.
This post has been edited by osu1: 07 August 2012 - 04:42 PM
#4
Re: Opening text files for input
Posted 07 August 2012 - 04:52 PM
ifstream fin("YourInputFileNameToProcess");
Quote
Not really, you are reading each individual character, not one complete line at a time.
Jim
This post has been edited by jimblumberg: 07 August 2012 - 04:53 PM
#5
Re: Opening text files for input
Posted 07 August 2012 - 05:02 PM
jimblumberg, on 07 August 2012 - 04:52 PM, said:
ifstream fin("YourInputFileNameToProcess");
Quote
Not really, you are reading each individual character, not one complete line at a time.
Jim
I am trying to Jim. It is not making full sense to me for some reason. Can you atleast give me a tip for reading the individual lines? Thank you.
#6
Re: Opening text files for input
Posted 07 August 2012 - 05:19 PM
osu1, on 07 August 2012 - 05:02 PM, said:
jimblumberg, on 07 August 2012 - 04:52 PM, said:
ifstream fin("YourInputFileNameToProcess");
Quote
Not really, you are reading each individual character, not one complete line at a time.
Jim
I am trying to Jim. It is not making full sense to me for some reason. Can you atleast give me a tip for reading the individual lines? Thank you.
Search for the words "Data input" in that link. You'll find the source code staring you in the face.
#7
Re: Opening text files for input
Posted 07 August 2012 - 05:24 PM
This post has been edited by JackOfAllTrades: 08 August 2012 - 03:35 AM
Reason for edit:: Removed unnecessary quote
#8
Re: Opening text files for input
Posted 07 August 2012 - 06:51 PM
This post has been edited by JackOfAllTrades: 08 August 2012 - 03:35 AM
Reason for edit:: Removed unnecessary quote
#9
Re: Opening text files for input
Posted 07 August 2012 - 07:03 PM
Jim
#10
Re: Opening text files for input
Posted 07 August 2012 - 07:06 PM
jimblumberg, on 07 August 2012 - 07:03 PM, said:
Jim
No Compile errors. Just cant input anything. I believed I have done everything i have read. I am opening 2 input files and merging the lines from them differently in 1 output file. Thank you.
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream intext; ifstream anothertext; ofstream output; string line[3]; // read line variable string mline[2]; intext.open("input.txt"); anothertext.open("input.txt"); output.open("out.txt"); cout << "Input into 1st file" << endl; intext >> line[3]; getline( intext,line[3]); cout << "Input intp 2nd file" << endl; anothertext >> mline[2]; getline(anothertext,mline[2]); output.open("blah.txt"); output << line[0] << mline[0] << line[1] << mline[1] << line[2] << mline[2] << line[4] << endl; intext.close(); anothertext.close(); output.close(); return 0; }
#11
Re: Opening text files for input
Posted 07 August 2012 - 07:12 PM
#12
Re: Opening text files for input
Posted 07 August 2012 - 07:19 PM
CTphpnwb, on 07 August 2012 - 07:12 PM, said:
I fixed that but line does has 4 elements.I switched line[4] to line[3]. Thank you but still didnt solve my input problem. I cant think of anything why it doesnt let me input.compiles just fine.
This post has been edited by osu1: 07 August 2012 - 07:22 PM
#13
Re: Opening text files for input
Posted 07 August 2012 - 07:31 PM
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream intext; ofstream output; string line[3]; // read line variable intext.open("input.txt"); output.open("out.txt"); for (int i = 0; i < 3; i++) { intext >> line[i]; } for (int i = 0; i < 3; i++) { output<<line[i] << endl; } intext.close(); output.close(); return 0; }
#14
Re: Opening text files for input
Posted 07 August 2012 - 07:32 PM
intext.open("input.txt"); anothertext.open("input.txt");
I recommend that you start by opening only one file. Read that file, and print the contents to the console (cout). Use a loop to read and print out your file. You also need to check that the file opened correctly.
You will be implementing code that is similar to the sample code in the link I provided earlier.
Once you can properly read a single file then you can move to the next step, writing to a file. Learn one small thing at a time. Once you have reading a single file, writing to another file, then you can work on reading multiple files.
Also please provide a sample of your input files.
Jim
#15
Re: Opening text files for input
Posted 07 August 2012 - 07:37 PM
CTphpnwb, on 07 August 2012 - 07:31 PM, said:
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream intext; ofstream output; string line[3]; // read line variable intext.open("input.txt"); output.open("out.txt"); for (int i = 0; i < 3; i++) { intext >> line[i]; } for (int i = 0; i < 3; i++) { output<<line[i] << endl; } intext.close(); output.close(); return 0; }
I see, but by doing this can I also use your method for the other text file? I have two input textfiles on my code. Also with your method I can print out the lines in different order like I attempted in my original code? I still dont see how my original code is wrong and wont let me input. Thank you for helping me out.
jimblumberg, on 07 August 2012 - 07:32 PM, said:
intext.open("input.txt"); anothertext.open("input.txt");
I recommend that you start by opening only one file. Read that file, and print the contents to the console (cout). Use a loop to read and print out your file. You also need to check that the file opened correctly.
You will be implementing code that is similar to the sample code in the link I provided earlier.
Once you can properly read a single file then you can move to the next step, writing to a file. Learn one small thing at a time. Once you have reading a single file, writing to another file, then you can work on reading multiple files.
Also please provide a sample of your input files.
Jim
Isn't intext and anothertext 2 different files to begin with? I thought I declared them as separately.I know how to read to a file, writing to a file is what I am seeing to be doing wrong. I cant input so i cant provide samples. Its the problem with my code.