hi there. say i have a text file. in this text file is all the information i need for my output. the problem is, probably because i'm still very new at this, i don't know how to read this particular text file and output it onto the screen. suppose i used,

inFile.open("data.txt")

to open the file.then i used

inFile >> a >> b ....and etc.

to read the text file. how do i output this information on screen?
i need it to look like this example,

source = a
destination = b

please help.

Recommended Answers

All 4 Replies

hi there. say i have a text file. in this text file is all the information i need for my output. the problem is, probably because i'm still very new at this, i don't know how to read this particular text file and output it onto the screen. suppose i used,

inFile.open("data.txt")

to open the file.then i used

inFile >> a >> b ....and etc.

to read the text file. how do i output this information on screen?
i need it to look like this example,

source = a
destination = b

please help.

If a and b are variables...

cout << "source = " << a << endl;
cout << "destination = " << b << endl;

Assumed that your input data is located in "data.txt" and that your data is seperated on a line by line basis, your sourcecode could be something like this:

#include <iostream>
#include <fstream>
#include <string>

void printContents(std::string a, std::string b)
{
	std::cout << "source = " << a << std::endl;
	std::cout << "destination = " << b << std::endl;
}


int main(int argc, char *argv[])
{
	char temp[256], tempp[256];
	std::ifstream in("data.txt");

	while(!in.eof())
	{
		in.getline(temp, 255);
		in.getline(tempp, 255);
		printContents(temp, tempp);
	}
	
	return 0;
}

whats the input file look like?

hi there. my data.txt file looks something like this.

a b c d
e f g h
i j k l

where each column represents different type of information. i did want to use array, but the lecturer wanted to test us to do it without using array. instead she asked us to declare all the information as variables. how do do this? i tried doing this,

inFile >> w1 >> x1 >> y1 >> z1>>w2>>x2>>y2>>z2>>w3>>x3>>y3>>z3>>w4>>x4>>y4>>z4;

where w,x,y,z represent different data.
and let's say,

if (input == 1)
cout << w1 <<x1<<y1<<z1<<endl;

and so on.
i can't output the data from data.txt onto the screen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.