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
//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.
}
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)
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
// 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.
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]
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()
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.
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=newchar[x+1];
sLine.copy(temp,x);
val=atoi(temp);
delete[] temp;
}
x++;
std::string name(sLine,x,sLine.find(',',x)-x);