Opening text files for input

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2831 Views - Last Post: 07 August 2012 - 11:52 PM Rate Topic: -----

#1 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Opening text files for input

Posted 07 August 2012 - 04:19 PM

Hello I am having problems opening up text files for input. I am not sure if I am in the correct path here since this still doesnt make much sense to me. I want to open 2 text files for input then merge them for output. This is what I have so far. Can someone please tell me if I am the right path? Thank you.


#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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Opening text files for input

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,742
  • Joined: 05-May 12

Re: Opening text files for input

Posted 07 August 2012 - 04:35 PM

Line 11 creates an array of 4 strings.

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.
Was This Post Helpful? 0
  • +
  • -

#3 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Opening text files for input

Posted 07 August 2012 - 04:41 PM

View PostSkydiver, on 07 August 2012 - 04:35 PM, said:

Line 11 creates an array of 4 strings.

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

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Opening text files for input

Posted 07 August 2012 - 04:52 PM

I think you should study up a little on basic file input and output. Also I recommend until you have a basic understanding of this topic you use a constant for your file name:
ifstream fin("YourInputFileNameToProcess");


Quote

But is my method for opening up the text file for inputing 4 lines correct?

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

Was This Post Helpful? 0
  • +
  • -

#5 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Opening text files for input

Posted 07 August 2012 - 05:02 PM

View Postjimblumberg, on 07 August 2012 - 04:52 PM, said:

I think you should study up a little on basic file input and output. Also I recommend until you have a basic understanding of this topic you use a constant for your file name:
ifstream fin("YourInputFileNameToProcess");


Quote

But is my method for opening up the text file for inputing 4 lines correct?

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.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,742
  • Joined: 05-May 12

Re: Opening text files for input

Posted 07 August 2012 - 05:19 PM

View Postosu1, on 07 August 2012 - 05:02 PM, said:

View Postjimblumberg, on 07 August 2012 - 04:52 PM, said:

I think you should study up a little on basic file input and output. Also I recommend until you have a basic understanding of this topic you use a constant for your file name:
ifstream fin("YourInputFileNameToProcess");


Quote

But is my method for opening up the text file for inputing 4 lines correct?

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.
Was This Post Helpful? 0
  • +
  • -

#7 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Opening text files for input

Posted 07 August 2012 - 05:24 PM

There it is. Thank you very much!

This post has been edited by JackOfAllTrades: 08 August 2012 - 03:35 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#8 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Opening text files for input

Posted 07 August 2012 - 06:51 PM

After reading and writing everything there is still a problem. I cant input into my program. Can you all view my code?

This post has been edited by JackOfAllTrades: 08 August 2012 - 03:35 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Opening text files for input

Posted 07 August 2012 - 07:03 PM

Post your current code, and if you are getting any compile errors post the complete error messages exactly as they appear in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Opening text files for input

Posted 07 August 2012 - 07:06 PM

View Postjimblumberg, on 07 August 2012 - 07:03 PM, said:

Post your current code, and if you are getting any compile errors post the complete error messages exactly as they appear in your development environment.

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;



}

Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Opening text files for input

Posted 07 August 2012 - 07:12 PM

For starters, line[3] does not exist because line is an array with three elements: line[0], line[1], and line[2].
Was This Post Helpful? 0
  • +
  • -

#12 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Opening text files for input

Posted 07 August 2012 - 07:19 PM

View PostCTphpnwb, on 07 August 2012 - 07:12 PM, said:

For starters, line[3] does not exist because line is an array with three elements: line[0], line[1], and line[2].


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

Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Opening text files for input

Posted 07 August 2012 - 07:31 PM

I think you're trying to do something like this:
#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;
}

Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Opening text files for input

Posted 07 August 2012 - 07:32 PM

Why are you opening the same file twice for input?
	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
Was This Post Helpful? 0
  • +
  • -

#15 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Opening text files for input

Posted 07 August 2012 - 07:37 PM

View PostCTphpnwb, on 07 August 2012 - 07:31 PM, said:

I think you're trying to do something like this:
#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.

View Postjimblumberg, on 07 August 2012 - 07:32 PM, said:

Why are you opening the same file twice for input?
	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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2