How to read a Files Data

Pages: 12
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
19
#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.
Last edited on
1
2
inputFile.open("Program4Data.docx");
if(!inputFile.is_open()) cout << "Error!!! The file Program4Data.docx not found!!!\n";
closed account (ENhkSL3A)
Honey you have to save the data to a file and name it that to use my program. It's trying to read the file. I posted the data.
Honey have you tried this line of code to see if the program actually opens the file as intended?
1
2
3
4
5
6
7
8
9
inputFile.open("Program4Data.docx");
if(!inputFile.is_open())
{
    cout << "Error!!! The file Program4Data.docx not found!!!\n";
}
else
{
    cout << "The file Program4Data.docx has been opened successfully.\n";
}
closed account (ENhkSL3A)
What are you trying to do?????????? You have to open a word doc and name it "Program4Data" then put in the data. Then run my program and see what it does? Why are you posting stuff that is not relevant?????
closed account (ENhkSL3A)
You had to of typed something wrong. Mine displays space that is it.
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.
Last edited on
closed account (ENhkSL3A)
Error!!! The file Program4Data.docx not found!!!
closed account (ENhkSL3A)
Does anyone have anything they know works? Also don't post if you didn't read my whole post please. I can assure you you're post will be useless if you don't read the original post to the end.
Last edited on
I have fulfilled my responsibility. Now I will let others step in.
closed account (ENhkSL3A)
Is that a joke? You literally post code that doesn't work. You've done this on three of my post and then get mad because I ask questions. You also tried to get me to pay you for code. Please just go on somewhere.
Did you create the file "Program4Data.docx" and put in the folder where your executable is?

P.S : I am not actually mad. It is just you are just being impatient.
Last edited on
closed account (ENhkSL3A)
Seriously. YES omg
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.
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.
Last edited on
Hello kns2872,

Let me try this: your original program wants to open the file "Program4Data.docx" which I understand as a Microsoft Word document. Do you know what is at the beginning of a ".docx" file? I am not exactly sure, but I believe that it starts with formatting information, so your first read is likely to return something that will not print. At this level of your experience you are not ready to deal with a full ".docx" file.

What you need is just a plain ".txt" file devoid of any formatting information. A simple program like notepad will work.

Next the inputFile>>name will only read until it encounters a white space. To read the entire line you could use getline(inputFile, name) or you could use this:
1
2
3
4
while (getline(inputfile, name)
{
// code here
}

to loop through the entire file. If you want to define individual variables for what is in the file you can use inputFile >> a >> b >> c; then that would make it easier to make use of the input.

Your original program works. I just saved the data to ".txt" file and used the while loop to read the file and it worked.

Hope that helps,

Andy
closed account (iE8M92yv)
Handy Andy has already replied with the main gist here: from the name of your input file (ends .docx) you have almost certainly created it with Microsoft Word and it needs to be a plain text file. If you don't want to type it again, cut and paste it into Windows notepad and save it (with extension .txt) from there. Change the name of the file in your program accordingly.

For files like this with number of lines not known a priori, I advocate reading whole lines into a string variable first, then stringstream'ing them into individual data variables. If you use getline (various forms) it can also test for end of file.

Try something like:
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
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <sstream>             // added line
#include <string>
using namespace std;

int main()
{
   ifstream inputFile;
   string line;                // changed variable name 
   double height, radius;      // added variables
   char calctype;              // added variable


   inputFile.open("Program4Data.txt");              // changed file name (and type)
   cout << "Reading data from the file. \n";

   // Read the first line (seems to be just characters / headers)
   getline( inputFile, line );
   cout << "First line is:" << endl;
   cout << line << endl;
   cout << endl;
   cout << "Data lines as follows:" << endl;
   cout << endl;

   // Read the subsequent lines (into a string called "line").
   // Getline will return false when there are no more and the loop will stop.
   // Take the numerical data out of string "line" by treating it as a stringstream
   while ( getline( inputFile, line ) )
   {
      stringstream( line ) >> height >> radius >> calctype;
      cout << "height = " << height << endl;
      cout << "radius = " << radius << endl;
      cout << "calctype = " << calctype << endl;
      cout << endl;
   }

   inputFile.close();
   return 0;
}



What you do with individual variables in the loop is up to you. (Just an example above.)
Last edited on
Hello kns2872,

While working with the program I found an error in the data file. line 7 has a coma where it should be a decimal point.

Andy
closed account (ENhkSL3A)
Alright guys I have my programs first bit done. Here is the files code that it is reading from:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ofstream outputFile;
	outputFile.open ("information.txt");
	string line1;
	string line2;
	string line3;
	string line4;
	string line5;
	string line6;
	string line7;
	string line8;
	string line9;
	string line10;
	string line11;
	
	line1 = "Height\t\tRadius\t\tCalculation\tType\n";
	line2 = "1.0\t\t1.0\t\tC\n";
	line3 = "1.0		1.0		V\n";
	line4 = "1.0		1.0		S\n";
	line5 = "1.0		1.0		D\n";
	line6 = "1010.0		250.0	C\n";
	line7 = "12.3		3.25	V\n";
	line8 = "123.0		43.0	S\n";
	line9 = "12.3		3.25	K\n";
	line10 = "1.12		2.13	C\n";
	line11 = "-999";
	
	outputFile << line1;
	outputFile << line2;
	outputFile << line3;
	outputFile << line4;
	outputFile << line5;
	outputFile << line6;
	outputFile << line7;
	outputFile << line8;
	outputFile << line9;
	outputFile << line10;
	outputFile << line11;
	
	outputFile.close();
	
	cout << "The data has been entered" << endl;
  
	return 0;
}

I realize there may be some extra coding in there but I'm not sure yet. I'm doing this homework one step at a time. I am going to now post the assignment instructions and then my program so far.

Instructions:
EGR 126
Program 4 – Modification and enhancement of an existing program

The objective of this Program is to revisit a previous program for modification and enhancement, specifically the addition of looping, input validation, and the use of a file for multiple inputs. Please review the description for Program 2, which calculated volume, circumference and surface area values for a right circular cylinder of specified size.

The assignment for this program is to modify the code from Program 2 so that the information and specifications for several different cylinders can be processed in one run using a while or do-while loop. Additionally, for each set of cylinder data, only one of the three values is to be printed out: volume, circumference OR surface area, not all three.

In order to specify only one type of calculation, not all three, an input parameter will be used to indicate which type of calculation to perform.

The option for determining the radius of a cylinder with the same height but a volume 50% greater is eliminated.

Design your program to read all the input values from a file. Set up your input file to contain the height, radius, and calculation type for each cylinder listed in sequential order. The first line of the file is a phrase that describes how the file is set up. Read this phrase and print it. The last entry in the file is a sentinel to indicate the end of data (something like -999). Do not use the sentinel as a value in your calculation.

Set up your data file to contain the following values to test your program. The first number in each line is the height, the second number is the radius, and the last number is the calculation type, where C is for Circumference, V is for
Volume, S is for Surface Area.

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

To determine the type of calculation to perform, the code should include a switch statement. You will switch on “character” based on the calculation type requested. Note that calculation type K and D are invalid types, so for those cases (or for any invalid type) print the input values, and an informative and appropriate message, and continue processing.

Keep count of the number of data sets successfully processed. (those with valid calculation types) You should include comments in your code.
You may choose to print to the screen or to an output file. Be sure to print the height, radius and requested calculation values, all appropriately labeled. Also print the number of data sets successfully processed. Follow our standard labeling requirements for name, program, date and purpose.

Use 2 decimal places of precision.

Be sure to turn in prints of the source code, the input file and the output.

Because we are testing the program with a fixed number of data sets, a for loop would probably be a better choice, but please use a while or do-while in this program.
Updated 8/20/2016

Here is my programs code:
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
34
35
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	ifstream inputFile;
	string word1;
	string word2;
	string word3;
	string word4;
	
	cout << "\t" << endl;
	cout << "\t" << endl;

	inputFile.open("information.txt");
	cout << "Reading File information. \n";
	
	cout << "\t" << endl;
	cout << "\t" << endl;
	
	inputFile >> word1;
	inputFile >> word2;
	inputFile >> word3;
	inputFile >> word4;
	cout << word1 << " " << word2 << " " << word3 << " " << word4 << endl;
	cout << "\t" << endl;
	cout << "\t" << endl;
	
	
	inputFile.close();
	return 0;
}
Pages: 12