I don't know what I'm doing wrong but I open my filed correctly but it doesn't display the contents in the file. Please help.

#include <iostream>
#include <fstream> 
using namespace std;

int main()
{
	
	const int SIZE = 81;
	char input[SIZE];
	fstream dataFile;
	
	dataFile.open("info.txt", ios::in);

	dataFile.getline(input,SIZE);


	system("pause");
	return 0;
}

Edited 5 Years Ago by Narue: Added code tags

First, please use [code] ...CODE tags... [/code], they make your code easier to read. See:

#include <iostream>
 #include <fstream> 
 using namespace std;

 int main()
 {

 const int SIZE = 81;
 char input[SIZE];
 fstream dataFile;

 dataFile.open("info.txt", ios::in);

 dataFile.getline(input,SIZE);


 system("pause");
 return 0;
 }

Second, whenever you are dealing with file streams, you should make sure the file actually is opening properly:

ifstream inFile;                        //declare the stream
char inputData[SIZE];                   //declare the buffer

inFile.open("myfile.txt", ios::in);     //open the file/stream
if (!inFile.is_open()) {                 //verify the open
  std::cout << "There has been an error opening the file.";
  return 1;                             //if not open, return an error
} else {
  inFile.getline(inputData, (SIZE-1));  //if open, read the file
}

Third, you may want to consider using an ifstream instead of an fstream, there is a difference. An ifstream will only perform input, an fstream will perform both input and output. Do you need the output functionality as well?

EDIT:
Fourth,
>>Please point out the line that should be displaying the contents of the file.
That too... :P:D

Edited 5 Years Ago by Fbody: n/a

to Fbody:
I thought line 12 states it functions as ifstream??

to Trustify:
not so sure how can you tell if it is working or not when there is no output

to Fbody:
I thought line 12 states it functions as ifstream??

to Trustify:
not so sure how can you tell if it is working or not when there is no output

No, it configures the stream to "allow input operations" and not "allow output operations", which is functionally similar to a ifstream, but it does not make it the same as a ifstream. The inheritance structure is different and they are different classes which means they are different types of objects with different system resource overheads.

According to the documentation I have access to, an ifsream inherits directly from istream. On the other hand, a fstream inherits directly from iostream, which in turn inherits from both istream and ostream (an indirect multiple inheritance).

Edited 5 Years Ago by Fbody: n/a

I was tring to read out the information that was in the info.txt file. For exmaple, I had wrote in that text file "abcdefg". So when I run my code I want to see "abcdefg" displayed on my console screen.

So I used

dataFile.getline(input,SIZE);

but I guess that didn't display my info from my text file.

So when I run my program I only get

"Press any key to continue...."

Edited 5 Years Ago by Trustify: n/a

I was tring to read out the information that was in the info.txt file. For exmaple, I had wrote in that text file "abcdefg". So when I run my code I want to see "abcdefg" displayed on my console screen.

according to my understanding, getline only copy the string to your input[]
you still need to cout it in order to see it on screen
might want to google it first, cplusplus.com is really helpful

Edited 5 Years Ago by bigwhiteegg: n/a

No, it configures the stream to "allow input operations" and not "allow output operations", which is functionally similar to a ifstream

I see... thanks for clarification
never thought the differences would be this huge

nvm I got it.

//read the file contents.
while (nameFile >> input)
{
cout << input;
}

This article has been dead for over six months. Start a new discussion instead.