0

I am newbie and have a C - Linux program that read a number of variable lines like this:

12 34 56 78
3 4 7 900
...
%d %d %d %d
...
1 2 3 4

The function I've written so far is like this:

void Import(const char* path_of_file)
{
  FILE *cfile = fopen(path_of_file, "r");
  int lines = 0;
  //I copied it from another question
  while (EOF != (fscanf(cfile,"%*[^/n]"), fscanf(cfile, "%*c"))) ++lines;
  fclose(cfile);
  cfile = fopen(path_of_file, "w");
  ...
  for( i=0; i<n; i++)
  {
    ...
    fscanf(cfile, "%d %d %d %d", &a, &b, &c, &d);
    ...
  }
  ...
  //Of course it will return something, but it shouldn't matter here
  fclose(cfile);
}

But may be because the text file is created in Windows or some thing, it doesn't always end with a newline, and so my program frequently miss the last line.

Is there anyway to automatic append a newline at the end of file if it's not there? Or better, is there any way to read the last line without that newline? A example function based on the one above will be very good.

apxcode
  • 7,696
  • 7
  • 30
  • 41
Tr1et
  • 873
  • 11
  • 25
  • 1
    if you're looking to use a Windows formatted file on a linux system, you may want to try the command-line tool dos2unix to fix the newlines – user3288829 May 21 '14 at 15:54
  • If you want to read lines, use fgets instead of scanf. If you have to worry about newline or not newline at the end of the file, you are probably doing it wrong. – Thomas Padron-McCarthy May 21 '14 at 15:56
  • Use `fgets` to read a line, and `sscanf` to parse it. Much easier handling. – Henrik May 21 '14 at 15:56
  • @user3288829 Actually, it's an assignment, so a tool is out of options, it must be ran within the program. – Tr1et May 21 '14 at 15:58
  • 1
    The way you calculate the number of lines is a bit strange (why `/n`?). But if it works most of the time, you can just overestimate the number by iterating `n+1` times and just exit the loop if `scanf` can't read anything. – Pavel May 21 '14 at 15:59
  • @Henrik Sorry,I am a newbie, can you write a small program use fgets, and why it will read that last line but fscanf can not? – Tr1et May 21 '14 at 16:01
  • @Pavel As I stated above, the count lines code is borrowed from another question, to be honest I didn't really understand the "%*[^/n]" and how two function can lie on the while clause with a "," between them. – Tr1et May 21 '14 at 16:06
  • is this where you got it from? http://stackoverflow.com/questions/4278845/what-is-the-easiest-way-to-count-the-newlines-in-an-ascii-file then check again – Pavel May 21 '14 at 16:07
  • @Pavel Yes, yes. That where I got it. And I have to pre-allocate a shared memory to store those number (in the ...), so try the 'n+1' step mean that I will have to reallocate all of it. – Tr1et May 21 '14 at 16:12
  • @Tr1et Check out this answer http://stackoverflow.com/questions/22383449/#22383633 – Henrik May 21 '14 at 17:34
  • @Tr1et re: "with a , between them" see [comma operator](https://en.wikipedia.org/wiki/Comma_operator). Both expressions are evaluated but only the result of the right-hand-side (the 2nd fscanf) is returned to the while clause. – RJFalconer May 22 '14 at 09:29

1 Answers1

0

Thank you Pavel, I have never read the other answer in that post, my fault for only read the Accepted answer. After read all those posts, I chosen to use this code to count the line, it can avoid the problem above:

int n = 0;
int pc = EOF;
int c;
while ((c = getc(cfile)) != EOF) {
  if (c == '\n')
    ++n;
  pc = c;
}
if (pc != EOF && pc != '\n')
  ++n;

Sorry for my stupidness.

Tr1et
  • 873
  • 11
  • 25