File I/O
| Vector (15) | |||
| 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 | |||
| Vector (15) | |||
| What can i use to read the file byte by byte? Hints are appreciated. ;) | |||
| helios (829) | |||
| Read the entire file into memory or line by line, then use strpbrk() to find the commas. | |||
| Vector (15) | |||
| 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 | |||
| Zaita (1450) | |||
| |||
| Vector (15) | |||
| 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 | |||
| QWERTYman (253) | |||
| fail() checks for both...um...failure...and EOF. As for what's wrong, I dunno. | |||
| Zaita (1450) | |||||
I will break my code down for you :)
Now, to read back the lines you can do.
| |||||
| Vector (15) | |||
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. | |||
| Zaita (1450) | |||
| Way off, inbyte is empty because your not reading anything. Btw: the tags are "code" not "source" to display the code
| |||
| Vector (15) | |||
| 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 | |||
| Vector (15) | |||
Nevermind i typed to soon.
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() | |||
| helios (829) | |||
| 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):
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. | |||
| Vector (15) | |||
| 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.
<sigh> | |||
| helios (829) | |||
| 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
| |||
This topic is archived - New replies not allowed.
