Task: Read the file line by line, the string is written in the character buffer. Each character is compared with the "correct" (I, J) character, after the character there is a number that is written into a variable divided by 1000 and displayed on the screen. Garbage symbols: F, R, H, Q and other slag is skipped.

I tried to nakodit, it turned out to bring only the numbers from the first line many, many times, and then nothing = (I’m getting my head on the table. I don’t understand what’s wrong. Please tell me who’s interested =)

Sample input file.

In it randomly there are different letters and numbers randomly.


N10I-255961J-829096F-360873 N11I-254238F-360873 N12J931921 N13Y-251413H-332067 N14I-653588 N15I248588F-332245 N16Q251413J-332085H-332067 N17I252145J331921 ..................... ..................... ..................... и так далее......... и так далее.........


My code.

 #include <fstream> #include <iostream> #include <ctype.h> #include <sstream> #include <stdio.h> int main() { int k=0; FILE *ifs = fopen("input.txt", "rt"); std::stringstream i,j; float i1=0,j1=0; char buf[1024]; while(fgets(buf,1024,ifs)){ while(buf[k]){ if (isalpha(buf[k])){ if ((int)buf[k]==73 && ((int)buf[k+1] > 44 && (int)buf[k+1] < 58)){//I do {i<<buf[k+1]; k++;} while (!isalpha(buf[k+1])); i>>i1; } if ((int)buf[k]==74 && ((int)buf[k+1] > 44 && (int)buf[k+1] < 58)){//J do {j<<buf[k+1]; k++;} while (!isalpha(buf[k+1])); j>>j1; } } else if (isdigit(buf[k])){ } k++;} std::cout <<"i" <<i1/1000<<std::endl; std::cout <<"j" <<i1/1000<<std::endl; } system("pause"); } 

Must be displayed

 i-255.961 j-829.096 j931.921 i-653.588 i248.588 i252.145 j331.921 
  • 1) check the source. There is clearly an error in one of the curly braces, not going. 2) Decide C or C++ . 3) Formulate the problem more precisely: print the number that immediately follows the characters I or J , skip the rest. The mark is taken into account. So? - PinkTux
  • @PinkTux Thanks for the rule. 1 Source checked, now it should be built. I am writing on the pros ( c++ ). 3 Yes, print the number after the characters I and J , skip the rest. Above is an example of how it should look. The mark is taken into account. =) - IronZeshadow
  • @PinkTux when writing a program in c++ you can use the c language, but not vice versa, set it if I am mistaken =) - IronZeshadow
  • They simply have different means. If this is part of a C ++ project, or a learning task on the pros, it makes sense to use positive tools. If the goal is just to parse the file, then at least write to awk ... But the code in the answer will be assembled as you like, both purely and positively. - PinkTux
  • Possible duplicate question: Input / Output to a file in C ++ - Andrey Buran

1 answer 1

In C (taking into account the clarifications in the question):

 #include <stdio.h> #include <string.h> int main( void ) { char c; long n; FILE *in = fopen( "in.txt", "r" ); if( !in ) { perror( "Can not open file" ); return -1; } while( fscanf( in, "%c%ld", &c, &n ) != EOF ) { if( strchr( "iIjJ", c ) ) { printf( "%c%.3f\n", ( c | 0x20 ), ( double )n / 1000.0 ); } } fclose( in ); return 0; } 

Conclusion:

 i-255.961 j-829.096 i-254.238 j931.921 i-653.588 i248.588 j-332.085 i252.145 j331.921