How to Start From the Second Row When Reading a File C++
11-06-2003 #1
Registered User
reading file from second line
hey guys I searched for this in the previous posts but couldn't find much. How would I make the plan read in a file starting from the second line?basically, the get-go line in the file tells me how many lines there will exist ii input. I read this in and brand an array of that size. Now I want to read in the rest of the file into that array starting from the 2nd line.
some entropy with that sink? entropysink.comthere are 2 cardinal sins from which all others jump: Impatience and 50aziness. - franz kafka
11-06-2003 #2
Code Goddess
>How would I brand the program read in a file starting from the second line?
Use getline to read the first line perhaps? Then you'll continue to read starting from the second line.
My best code is written with the delete cardinal.
11-06-2003 #iv
Registered User
Thanks guys, I figured out a bit different way to do it....at present I have another problem...the file has character as well as numbers. The numbers could be anything from ints to floats. I simply want to pick out these as I'chiliad reading the file in. I know how to code it in C only I'grand not sure how the c++ code would look similar.my c version:
this reads decimals simply thought.Lawmaking:
int main(int argc, char *argv[]) { int line, con1, con2, i; char buffer[100]; if (argc != 2) { cout << "Commandline Error!"; exit (1); } else { ifstream examplefile(argv[1]); //open a file indicated by the single command //line argument if (! examplefile.is_open()) { cout << "Mistake opening file"; exit (1); } examplefile.getline (buffer,100); sscanf(buffer,"%d", &line); cout << line << endl; for (i=1; i<=line; i++) { examplefile.getline (buffer,100); sscanf(buffer, "%d, %d", &con1, &con2); cout << con1 << "," << con2 << endl; } return 0; } }
some entropy with that sink? entropysink.comat that place are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka
11-06-2003 #5
Registered User
i call back I well-nigh figured information technology out...but at present another problem arises. When stepping through the file looking for the ints the loop counter goes upward...when I practise find an int I desire the array to commencement at 0, if you get my meaning.SORRY the cde is awfully muddy.
EDIT:: the multiplication by iii is considering my input is in the following style (i, 2, three) (4, 5, 6)....and so if in that location are 3 more than lines following i will have a total of 3*3 inputs.Code:
ifstream inFile; ofstream outFile; cord inFileName, outFileName; int numberOfBuildings; bladder descriptions; char tmp; int i = 0; int j = 0; inFile.open("hey.txt"); inFile>>numberOfBuildings; cout << numberOfBuildings << endl; int *B = new int[numberOfBuildings*three]; for( i = 0; i < (numberOfBuildings*3); i++ ){ inFile>>tmp; if( isdigit(tmp) ) { B[i] = (int)tmp; continue; } }
some entropy with that sink? entropysink.comthere are two primal sins from which all others spring: Impatience and Fiftyaziness. - franz kafka
11-06-2003 #half dozen
Registered User
Homo AM I RUSTY!! I'm taking a information structures and discrete mathematics form, and this is our first prog of the semester....I really forgot how to do the syntax, yet I could write a whatsoever kind of search you would like....lol. As they say practice makes perfect.equally I was posting my last message it hit me....here is what I did, still dirty thought.
Lawmaking:
ifstream inFile; ofstream outFile; string inFileName, outFileName; int numberOfBuildings; float descriptions; char tmp[one]; int i = 0; int j = 0; inFile.open("hey.txt"); inFile>>numberOfBuildings; cout << numberOfBuildings << endl; int *B = new int[numberOfBuildings*3]; for( i = 0; i < (numberOfBuildings*7); i++ ){ inFile>>tmp[0]; if( isdigit(tmp[0]) ) { cout << tmp[0] << " :: " << endl; B[j] = atoi(tmp); j++; } } for( i = 0; i < numberOfBuildings*three; i++ ){ cout << B[i] << endl; }
some entropy with that sink? entropysink.comat that place are two cardinal sins from which all others spring: Impatience and 50aziness. - franz kafka
Source: https://cboard.cprogramming.com/cplusplus-programming/46858-reading-file-second-line.html
0 Response to "How to Start From the Second Row When Reading a File C++"
Postar um comentário