Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readLine returns null prematurely #100

Open
ColinEberhardt opened this issue Dec 6, 2020 · 3 comments
Open

readLine returns null prematurely #100

ColinEberhardt opened this issue Dec 6, 2020 · 3 comments
Labels

Comments

@ColinEberhardt
Copy link

I'm using the following to load a multi-line file into a string array:

  const fileOrNull: Descriptor | null = FileSystem.open(filename, "r");
  if (fileOrNull == null) {
    throw new Error("Could not open the file " + filename);
  }
  const file = changetype<Descriptor>(fileOrNull);

  const lines = new Array<string>();
  let line = changetype<string>(file.readLine());
  do {
    lines.push(line);
    line = changetype<string>(file.readLine());
  } while(line)
  return lines;

However, give the following input:

one
two
three

The result is an array ["one", "two"] - the readLine method appears to return null prematurely

@MaxGraey
Copy link
Contributor

MaxGraey commented Dec 6, 2020

Btw you could nicer cast to non-nullable type without changetype:

  const file = FileSystem.open(filename, "r")
  if (file == null) {
    throw new Error("Could not open the file " + filename)
  }
  const lines: string[] = []
  let line = file!.readLine()
  do {
    lines.push(line!)
    line = file!.readLine()
  } while (line)
  return lines

btw isn't this should be?

const lines: string[] = []
while (true) {
  let line = file!.readLine()
  if (line == null) break
  lines.push(line!)
}
return lines

Regarding issue when readLine return null prematurely it hard to tell.

@jedisct1 btw here should be if (read == 0) here due to read is unsigned type

@ColinEberhardt
Copy link
Author

Btw you could nicer cast to non-nullable type without changetype

Good call, I'm not that fluent in TS, so forgot about the non-null assertion operator.

btw isn't this should be? ...

Your code gives the same result, the last line is skipped.

Copy link

github-actions bot commented Jan 1, 2025

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the Stale label Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants