someDude
someDude

Reputation: 21

Handling CRLF, CR and LF when reading a txt file

I have a portion of C-code as below which is complied in linux gcc environment.

In my project I have to handle ALL CRLF, CR and LF when reading a txt file created from different OS.

I'm not sure if fscanf() handles all cases automatically.

Is there any other way that can handle all cases?

while (fscanf(fp, "%d", &data) != EOF) 
{
    printf("%d\n", data);
}

Upvotes: 1

Views: 1900

Answers (1)

chux
chux

Reputation: 154075

to handle ALL CRLF, CR and LF when reading a txt file created from different OS.

I'm not sure if fscanf() handles all cases automatically.

Some usage of fscanf() will work fine like fscanf(fp, "%d", &data), but not all.

A simple alternative is to read lines of input with your own my_fgets(), and then call sscanf().

char my_fgets(char *s, size_t sz, FILE *fp) {
  if (sz < 1) {
    return NULL;
  }
  char *org = s;
  bool no_input = true;
  int ch = 0;

  while (--sz > 0 && (ch = fgetc(fp)) != EOF) { 
    no_input = false; 
    if (ch == '\r') {
      int ch2 = fgetc(fp);
      if (ch2 != '\n') ungetc(ch2, fp);
      break;
    }
    if (ch == '\n') {
      break;
    }
    *s++ = ch;
  }
  *s = '\0';
  if ((ch == EOF) && (no_input || !feof(fp))) return NULL;
  return org;
} 

my_fgets(buffer, sizeof buffer, fp);
sscanf(buffer, ...);

This will handle most situations should the file be open in binary or text mode.


Relying on text mode and system-dependent line-ending translation is insufficient as code needs to handle at least 3 cases, some of which might not correspond to the expected system-dependent line-ending.

Upvotes: 1

Related Questions