Skip to content Skip to sidebar Skip to footer

How to Skip a Line in C File Reading

Skip 3 lines from a file while storing into array

I'm doing an assignment for school and I'm stuck. From the following .txt file:
John
100
99
100
Julio
88.v
eighty.five
85
Johnathan
70
83.5
86
Julia
80.five
89.ix
93

I demand to skip the grades to shop only the names in the array ""string names[NAMES_SIZE]""

                          one
2
3
4
5
6
vii
8
9
x
eleven
12
xiii
14
xv
16
17
18
xix
20
21
22
23
24
25
26
27
28
29
thirty
31
32
33
34
35
36
37
38
39
40
                                                      int                            chief() {                            const                            int                            max_line = 65536;                            const                            int                            NAMES_SIZE = iv;                            const                            int                            GRADES_SIZE = 3; 	string names[NAMES_SIZE];                            double                            grades[NAMES_SIZE][GRADES_SIZE]; 	ifstream inputFile; 	cord fileName;                            int                            count = 0;                            int                            grade = 0;                            int                            student = 0;  	cout <<                            "Please enter the file name.\n"; 	cin >> fileName;  	inputFile.open(fileName);                            while                            (!inputFile) 	{ 		cout <<                            "File open failure!"; 		cout <<                            "Please enter the correct name"                            <<                            " for the file"; 		cin >> fileName; 		inputFile.open(fileName); 		count++;                            if                            (count == 2) 		{ 			cout <<                            "Wrong file proper name"; 			get out(1); 		} 	}                            //Aid on this part!!                            for                            (count = 0; count < NAMES_SIZE; count++) 	{ 		inputFile >> names[count]; 		cout << names[count] << endl;                            //something here to skip the lines with the grades then motility on to reading into the parallel array                            }                        

Thank you in accelerate.

I tin can think of 2 simple means to practice it.

Manner i:
You read the file line past line using a counter starting at 0 and increment it by 1 later on the line was read.
If the counter is 0, iv, 8...it is a proper name and you use information technology.

Way two:
You read the file line past line and bank check if the line is a number. If information technology is not a number then it must be a name and you can use information technology.

I'g a beginner and my lawmaking knowledge is very poor, would y'all please prove your caption in code, if you used the same variables names equally I did would be really helpful. Cheers

Concluding edited on

A suggestion: since you have to store all the values in your two arrays:

                          i
2
                                                      cord names[NAMES_SIZE];                            double                            grades[NAMES_SIZE][GRADES_SIZE];                        

then there'southward no demand to skip annihilation. Just read all of the data in the social club in which it appears in the file.

In outcome, something similar this:

                          1
2
3
4
five
6
seven
8
9
ten
xi
12
13
                                                      int                            count = 0;                            while                            (count < NAMES_SIZE && inputFile >> names[count])     {         inputFile >> grade1;         inputFile >> grade2;         inputFile >> grade3;                            // increment the count only after successfully                                                        // reading the proper noun and iii grades                            if                            (inputFile)             count++;     }                        

The while loop condition is worth a closer look, first it checks that count is not bigger than the array size. And then it attempts to read a name. If that succeeds, the body of the loop will then exist executed. But y'all however need to check the status before incrementing the count - if something goes incorrect, you lot demand to know how far it got before failure. After the loop completes, the count will contain the number of complete sets of data

Note besides, where I put grade1 etc, yous need to put the other assortment in there instead.

Last edited on

Every bit already suggested, one of many possible variants:

                          i
two
iii
four
5
6
seven
viii
9
x
11
12
13
14
fifteen
                                                      //Assist on this role!!                            double                            dummy = 0;                            int                            no_to_miss = 3;                            // Just to generalize it if ever needed                            count = 0;                            while(inputFile >> names[count])     {                            for(int                            i = 0; i < no_to_miss; i++)             inputFile >> dummy;                  count++;     }                            // Examination                            for(int                            i = 0; i < count; i++)         cout << names[i] <<                            '\n';                        
                  Please enter the file name. skip_items.txt John Julio Johnathan Julia Program concluded with leave code: 0                

Topic archived. No new replies allowed.

mckaytheran.blogspot.com

Source: http://www.cplusplus.com/forum/beginner/219155/

Postar um comentário for "How to Skip a Line in C File Reading"