Switch to full style
For C/C++ coders discussions and solutions
Post a reply

C++ File I/O

Sat Mar 28, 2009 3:28 pm

Well, it's the weekend, and my program due for class tomorrow isn't finished. The instructor isn't likely going to be available, so I'm hoping someone on here might be able to help out with a problem I'm having. I'll give the general layout of the program I am writing:

We are to pull from a text file the following information, in this format:

New York
0.075 0.06 0.081 0.056
0.065 0.068 0.07 0.059
0.07 0.075 0.068 0.073

The location is of course listed at the top in string format, and all of the individual numbers are ppm readings. The first in each row is a standard, the following three in that row are readings from sensors placed throughout the location. My programs purpose is to pull this information from the file, compare the readings to the standard, decide whether or not each polutant is in compliance with set standards.

Let's say this is in a simple notepad text file laid out as such. I forgot to mention, in my instructions at the end of each row there could be uneeded information, so I would need an ignore line for each row I would guess. This is how I have the first part of my program laid out, and just to test to see if the values are actually being pulled, I stuck in a section of COUT that isn't needed in the final program. Here is what I have :
cpp code
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()

{

/*For simplicity sake, I am only using the

first two rows of variables! These aren't all I will need for the

entire program, I am aware of that. Just to show what

the problem I'm having is.*/

float ozstd;
float oz1;
float oz2;
float oz3;
float nitstd;
float nit1;
float nit2;
float nit3;
string fileName;

ifstream inFile;

cout << "Please enter the name of the file to be used: ";
cin >> fileName;

inFile.open(fileName.c_str());

/*I'm not really sure how to pull the string name yet either.

But this is for the numerical values*/

inFile >> ozstd >> oz1 >> oz2 >> oz3;
inFile >> nitstd >> nit1 >> nit2 >> nit3;

inFile.close();

cout << "TEST to see if values are pulled from file " << ozstd << " " << oz1 << " " << oz2 << " " << oz3 << endl;

return 0;
}


When I compile this, everything runs fine, except for one thing. I get this random string of numbers in my output AND, I can type any filename into the prompt, I will get the same set of numbers in output. Lets say the file name is TEST1.txt That's the exact way I type it in, but I could also type TST1.txt and get the same result. I believe it's just not opening and reading from the file. Any ideas at all would be greatly appreciated! Thanks!



Post a reply
  Related Posts  to : C++ File I/O
 Encrypt/Decrypt a file from source file to target file.     -  
 Copy file to file in java code- implementation     -  
 file descriptor vs file pointer     -  
 getting file name of html input file tag using jsp     -  
 How to convert xml file to Pdf file using C     -  
 web.xml file     -  
 explode CSV file     -  
 JSP to Download File     -  
 Append to file using php     -  
 NS-2 and Trace file     -  

Topic Tags

C++ Files and I/O