So I am trying to read in the first line from a file. When I run the program it just displays blank lines. WHY?? Here is the info in the file:
Height Radius Calculation Type
1.0 1.0 C
1.0 1.0 V
1.0 1.0 S
1.0 1.0 D
1010.0 250.0 C
12.3 3.25 V
123,0 43.0 S
12.3 3.25 K
1.12 2.13 C
and here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
ifstream inputFile;
string name;
inputFile.open("Program4Data.docx");
cout << "Reading data from the file. \n";
inputFile >> name;
cout << name << endl;
return 0;
}
I am new at programming. I've only been doing it a couple months. So please understand if I ask a silly question or want you to explain anything.
You are not helping me. This code didn't work.
Please PLEASE PLEASE explain things. It makes zero since when you just post some code. I want to KNOW what I am doing and educate myself as I go. Not just plug and chug what people say.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
ifstream inputFile;
string name;
float Height, Radius;
char type;
inputFile.open("Program4Data.docx");
cout << "Reading data from the file. \n";
getline(inputFile , name); // Reads in whole line
cout << name << endl; // Prints
while (inputFile >> Height >> Radius >> type) // Keep reading until end of file and no more data
{
cout << " " << Height << "\t\t " << Radius << "\t\t " << type << endl; // Prints out what was read. '\t' ate tab functions
}
return 0;
}
Thank you so much for giving me an answer to my question. Also, for explaining everything. It makes a lot more since now. I appreciate you also taking the time to read my question to its entirety.
You're welcome. But, sadly, I messed up a bit. I should have closed the file when it was at the end. So, on a line before return 0;, you should add inputFile.close();.
Also, if you run this program without the IDE, the console will close before you can see anything printed on screen. You could add a cin>>type; just before the return but after the close statement. This will keep the console open.
When I run it in Terminal the output is "reading data from file."
That's it :/ My file has the data. I don't get why it won't read it. It has the right name and all. I have no idea what to do.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
ifstream inputFile;
string name;
inputFile.open("Program4Data.txt");
if(!inputFile.is_open())
{
cout << "Error!!! The file Program4Data.txt not found!!!\n";
cin.get(); return 0;
}
else
{
cout << "The file Program4Data.txt has been opened successfully.\n";
}
cout << "Reading data from the file... \n";
while(getline(inputFile, name)) { cout << name << endl; } // (1)
// int n = 0;
// while(inputFile >> name) { cout << name << ' '; if(++n == 3) { cout << endl; n = 0; } } // (2)
cin.get();
inputFile.close();
return 0;
}
The file Program4Data.txt has been opened successfully.
Reading data from the file...
1.0 1.0 C
1.0 1.0 V
1.0 1.0 S
1.0 1.0 D
1010.0 250.0 C
12.3 3.25 V
123,0 43.0 S
12.3 3.25 K
1.12 2.13 C
There are two methods of reading. Pick the favorite one of your choice.
P.S : I also added cin.get() at the end of the program.
I get it. You are trying to read an actual docx file. It is definitely a different type of file, not a plain text file.
So create a plain text file with the following :
1.0 1.0 C
1.0 1.0 V
1.0 1.0 S
1.0 1.0 D
1010.0 250.0 C
12.3 3.25 V
123,0 43.0 S
12.3 3.25 K
1.12 2.13 C
Name the file as "Program4Data.txt" and tell the program to load it.
Even so, your program is telling you that the file you request cannot be found.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream InputFile;
std::string Name;
// Make this file to a .txt file. .docx might not work
InputFile.open("Program4Data.docx");
std::cout << "Reading data from the file.\n";
// Use getline instead of cin to get whole line
getline(InputFile, Name);
std::cout << Name << std::endl;
return 0;
}
1. Change the .docx file to a .txt file just in case.
2. Use getline() function instead of cin to get the whole line.
3. Make sure that "Program4Data.txt" is in the same location as your program OR specify the location of "Program4Data.txt" like InputFile.open("C:\\Users\\InsertHere\\etc.\\Program4Data.txt");
(you need to use double "\\" since to use backslash you need to use escape character ["\\" = "\"]).
I wish I could block SakurasouBusters because they don't know what they are doing and they just post the same stuff over and over.
Okay guys. I tried using a txt file. It didn't work.. I then tried writing to an output file and that didn't work. I'm about to contact my teacher on this one. Also, boost lexical cat, it is finding the docx and txt that's not the problem. It just won't read anything in and output it to the user. All it outputs is lines of space :(
I messed up my output file. I'm fixing it. It should work now. Thanks for your help guys.
Of course, as you found out, docx files don't work, since there is a lot of information embedded in the file, like font type, font size, font colors, program info that created the file etc. I'm supplying a straight, text file. Hope all goes well.