Help Please.

closed account (ENhkSL3A)
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>
using namespace 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.
closed account (ENhkSL3A)
Please do not reply with new code and no explanation and READ THE WHOLE POST PLEASE!!
Please do not double-post.
www.cplusplus.com/forum/general/199078/
Why not try this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	
	ifstream inputFile;
	string name;

	inputFile.open("Program4Data.docx");
	if(!inputFile.is_open())
	{
 	   cout << "Error!!! The file Program4Data.docx not found!!!\n"; return 0;
	}
	else
	{
	    cout << "The file Program4Data.docx has been opened successfully.\n";
	}
	cout << "Reading data from the file... \n";

        int n = 0;
	while(inputFile >> name) { cout << name << ' '; if(++n == 3) { cout << endl; n = 0; } }
	
	return 0;
}


Show us the output you get.
closed account (ENhkSL3A)
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.
http://www.cplusplus.com/forum/general/199078/

P.S : Did you understand the error message you get?
Last edited on
@kns2872

Hopefully, this helps you out. I added in some comments, but if you have more questions, please ask.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>

using namespace 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;
}
closed account (ENhkSL3A)
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.
Last edited on
@kns2872

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.
closed account (ENhkSL3A)
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.
Last edited on
closed account (ENhkSL3A)
/Users/kaitlinstevers/Desktop/Screen Shot 2016-10-05 at 1.01.04 AM.png
closed account (ENhkSL3A)
file:///Users/kaitlinstevers/Desktop/Screen%20Shot%202016-10-05%20at%201.01.04%20AM.png
Please note that other people are also trying to help you. Ignoring one of them on purpose is not a good way of getting help.

Your feedback is needed so that people can help you further.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
#include <string>
using namespace 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.
Last edited on
Also, read this carefully :
SakurasouBusters wrote:
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.
Without adding anything new to your code I would change your code like this:
(P. S. Explanation below)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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 ["\\" = "\"]).
Last edited on
@boost lexical cast
It has been constantly stated that the OP is currently having trouble opening the file, let alone reading the file.
closed account (ENhkSL3A)
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.
Last edited on
@kns2872

I've uploaded the source file and the necessary txt file that I've been using. They are located in my dropbox. Here are the links..

https://www.dropbox.com/s/inqjkw9746m9a53/Radius%20program.cpp?dl=0
https://www.dropbox.com/s/ctuo7q0amkdtdco/Program4Data.txt?dl=0

These work for me.

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.
So even others' solutions didn't work and you are blaming me? How funny.
Should I even have to send you my entire solution with an executable as a real evidence?

It is too bad, you did not even listen to others' advice (including mine) and put the input file in the right place.

Chatting would be better so that you don't waste your time, if I have to say.
Topic archived. No new replies allowed.