1

Basically a line looks like this: 'number number text text text' with spaces dividing them. The numbers are ok, because the readln() just splits them after the space, but it reads the 3 texts as one. How can i read them into separate strings?

7
  • You probably meant that you used read() instead of readln(). readln() always reads until the end of the line (also for numbers). Unfortunately for you read() for a string also reads to the end of the line (unlike for numbers). So the only solution for you would be to split the text once you read it, in code. Commented Apr 17, 2015 at 17:40
  • 3
    You can use sscanf function from SysUtils unit. Just read whole line using ReadLn and then split it to the variables using sscanf. Commented Apr 17, 2015 at 18:47
  • More Pascal like is to use TStringList.delimtedtext Commented Apr 18, 2015 at 12:41
  • @Rik Yeah, I read the whole file char by char and split it at spaces, but it was a really long code and I wanted to do it more effectively (this is for an exam haha) Commented Apr 18, 2015 at 18:24
  • @Abelisto Thank you, I'll try both and see which one works for me c: Commented Apr 18, 2015 at 18:24

1 Answer 1

1

If anybody faces this problem, here's a really easy solution I just found: read the whole thing into a string. Then pos(' ',stringsname), then copy('spacepos'+1, 200), then delete(spacepos,200) from the first string and voilá.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.