File I/O

hiYa! Im new to reading files with C++! With C im A-OK!

Im Reading the same file as before! Only this time its problematic for me to use fgetc(filePointer). In C i started with reading the file byte by byte, using a character varaible then a nested for loops inside a while that tests for EOF!

The lines of myFile look similar to this:
0000003,V-Calculus for Polly,56.70.345.60

here is some code:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

main()
{
int digits[256], // arrays for what should be 'single digits'
deci[256], // for decimals
dec2[256],
myNum, // may be used as index
myNum2;

char inByte,
myStr[654]; // for the sentence strings

string myFile;
ifstream inFile; // fstream for both ifstream for input

cout << "Please enter Filename: ";
cin >> myFile; // must enter a file on computer


inFile.open(myFile.c_str(), ios::in); //open file for reading

if (!inFile) // validating the file
{
cout << myFile << " is not on Disk!";
return 1;
}

inFile.get(inByte); // read a char from file

while ( !inFile.eof() && !inFile.fail() ) // iterate if not fail or EOF
{
// mychar = ?

if (inByte == ',')break;

cout << inByte; // display character(s)
inFile.get(inByte);
}

//cout << myChar;
inFile.close(); // not done tho :p


system("pause");
return 0;
}


******************************
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
What i want to do at this point, since im only reading the first str b4 the comma, is to store and convert it to an integer. But my goal is to read each 00001, hit that comma and go to the next line until EOF. I've tried using a
for(int i=0; i<50; i++)
{
myStr[i]; // store all the numbers before teh commma.
}

But its difficult for me im new ;p
What can i use to read the file byte by byte? Hints are appreciated. ;)
Read the entire file into memory or line by line, then use strpbrk() to find the commas.
Last edited on
fstream inFile;
string myFile;

i read the entire file with, getline (inFile, myFile);

How can i put that into an array when im not reading byte by byte? Even so myFile is the string enabling the file to be read. Arrays cannot be used for as a getline paramater.

is there a equivalent condition to this? while ((inByte = fgetc(fp)) != EOF)

~Can u be a little more detailed

1
2
3
4
5
6
7
8
9
ifstream fConfig(FileName.c_str());
string sLine = "";
vector<string> vLineList;

while (getline(fConfig, sLine)) {
 if (sLine.length() == 0)
  continue;
 vLineList.push_back(sLine);
}
i wish i could use what your trying to tell me. ;p

Yes it does read the file! But i need a EOF and fail check condtion.Although your pushback is quite nice.
while(!inFile.fail() && !inFile.eof())
{
myLine.push_back( inFile.get() ); // file read
myStr[myInt++] = inByte; // compile ERROR!
for (int myInt = 0; myInt < 250; myInt++)
{
if (myLine[myInt] == ',') break;
myStr[myInt++] = myLine;
}
// store numbers in array
// convert to single digit
}

Ok what i guess is happening is that im reading the file using a string varaible 'myLine'. Then storing up to 251 characters and storing them in an array. err, thats what i want to happen :p

fail() checks for both...um...failure...and EOF.
As for what's wrong, I dunno.
I will break my code down for you :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Open the file
ifstream fConfig(FileName.c_str()); 

// Check if file opened ok
if (!fConfig)
 // handle error

// Create string variable to hold each line
string sLine = "";

// Create vectory *dynamic array* of strings
vector<string> vLineList;

// Loop continually getting each line and putting it into sLine
while (getline(fConfig, sLine)) {

 // if sLine is empty, get next line
 if (sLine.length() == 0)
  continue;

 // add the line to our array
 vLineList.push_back(sLine);
}


Now, to read back the lines you can do.

1
2
for (int i = 0; i < (int)vLineList.size(); ++i)
 cout << "Line " << i << ": " << vLineList[i] << endl;
[source]
while (getline(inFile, sLine)) // loop continually storing ch's into sLine
{
sLine = inByte;
if(inByte == ',')break;
sLine = inByte;
if (sLine.length() == 0)continue; // if empty get next line
vLineList.push_back(sLine); // adding lines to array

}
// your for loop w/o curls
// cout vLineList[myNum]

[/source]


but i get blanks.
should inByte have an array index in teh if state? or am i waayyy off.
Last edited on
Way off, inbyte is empty because your not reading anything.

Btw: the tags are "code" not "source" to display the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (getline(inFile, sLine)) // loop continually storing LINES into sLine
{
sLine = inByte; // Your over-writing sLine with nothing?
if(inByte == ',')break; // Don't want this yet.
sLine = inByte; // Don't want this either
if (sLine.length() == 0)continue; // if empty get next line
vLineList.push_back(sLine); // adding lines to array

}

// At this point, you have a vector (array) with each non-blank line from the file.

// your for loop w/o curls
// cout vLineList[myNum] 

Well, i figured out how to read the lines of the file individually.

cout << vLineList[0] << endl; // reading first line

so how would i check for comma's in that string?

i dont know what to have in the next for loop
Nevermind i typed to soon.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

 while (getline(inFile, sLine))   // loop continually storing LINES into sLine
    {
          
          if (sLine.length() == 0)continue;    // if empty get next line
          vLineList.push_back(sLine);      // adding lines to array
          
          for (int idx = 0; idx < 1024; idx++)   // dont want to use 1024
          {
              if (sLine[idx] == ',')break;       // store into idx numbers b4 comma
              cout << sLine[idx] << endl;     
          }
              
    }


but i dont want to use 1024 cuz they can brake it. also vLineList.size() doesnt work there, which is what i was trying :p

Also i need to convert these numbers to single digits, is that still possible with <ctype.h> atoi in C++?

Let me correct myself. Vlinelist does work in the for loop. But loses the '1' in 00001.
What the atoi() did with ctype.h was, i guess, trucated the '0's and left the 1 alone.
I need a similar function to do that. otherwise i'll have to use 1024 in the for loop insted of vlinelist.size()



Last edited on
Um... idx<sLine.length()?
Since you're already reading character by character, you could use (I'm assuming you want the whole string before the comma as a single value, not every digit in a different value):
1
2
3
4
5
//This goes before the loop:
int val=0;
//Inside the loop:
val=val*10+sLine[idx]-'0';
//After the loop, val has the integer representation of the string. 

I know. Not the best solution, system-dependent, blah-blah-blah. It will work as long as the code page encodes the decimal digits in contiguous code points. Both ASCII (and therefore Unicode) and EBCDIC follow this rule, so f*** it.
Last edited on
If you could find a function that is equivalent to atoi, or atof for me that'd be cool.

But as for the progress im struggling to read the string seperated by whitespace.
trying a nested for in a for nested in the while.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 while (getline(inFile, sLine))   // loop continually storing LINES into sLine
    {
          
       if (sLine.length() == 0)continue;    // if empty get next line
       vLineList.push_back(sLine);      // adding lines to array
          
          for (int myNums = 0; myNums < vLineList.size(); --myNums)
          {
              if (sLine[myNums] == ',')break;
              // cout << sLine[myLines] << endl; // storing nums before comma
              
                 for (int myStr = 0; myStr < vLineList.size(); --myStr)
                 {
                     if (sLine[myStr] == ',')continue;  
                     if (sLine[myStr] == ' ')continue;
                     if (sLine[myStr] == ',')break;
                     cout << sLine[myStr];
                 }
          }


<sigh>
You know, this site has a reference for a reason. Five minutes ago I didn't know how to do this.
What you want is to get this substring, right?
0000003,V-Calculus for Polly,56.70.345.60
1
2
3
4
5
6
7
8
9
10
11
std::string sLine="0000003,V-Calculus for Polly,56.70.345.60";
int val;
size_t x=sLine.find(',');
{
	char *temp=new char[x+1];
	sLine.copy(temp,x);
	val=atoi(temp);
	delete[] temp;
}
x++;
std::string name(sLine,x,sLine.find(',',x)-x);

Topic archived. No new replies allowed.