Hello.

The program reads the file and prints the contents on the screen.

program aaa; var f: text; s: string; i: integer; begin assign(f, '1.txt'); reset(f); while not eof(f) do begin readln(f, s, i); writeln(s, ' ', i); end; end. 

But when you try to run PascalABC.NET, it says:

 Ошибка времени исполнения: Входная строка имела неверный формат. 

What could be the error? Input file content:

 mynamw 334 myeemw 334 mynrrramw 334 
  • By the way, you read many times, and output to the console only once, the last line of the file (if it is non-empty). Is this a bug? - VladD
  • Yes, it turns out, ate a string. Conclusion: "mynrrramw 334 100". - maxsynergy
  • Corrected, must read from file and print. - maxsynergy

2 answers 2

I would venture to suggest that readln proudly counted "mynamw 334" in s , and could not find anything like i . How does he know where your line ends and are there any spaces in it? Try replacing it with just readln(f, s); i := 100; readln(f, s); i := 100; and see what happens.


So, the problem is found. And you can rearrange the data:

334 mynamw
334 myeemw
334 mynrrramw

?

If yes, then everything will turn out with readln(f, i, s); .


Since we decided not to change the format, the task becomes more difficult :) I would consider the entire line to be a string variable ( readln(f, line); ), and then I would find the last space in it (search from the end) and divide it by s ( the substring from the beginning to the position of the found space) and i (the rest of the string, translate into a number using StrToInt ). It is clear how?

  • This is how it all works, but I would like it to have a text string (of variable length) at the beginning. - maxsynergy
  • @maxsynergy: updated answer - VladD
  • Understood, it is necessary to break all the same into substrings. - maxsynergy
  • @maxsynergy: Yeah, since readln can't handle it. - VladD
  • I see. Thanks! - maxsynergy

ReadLn procedure help