reading input from a system call

A

Anonymous

jan said:
So even this seems like a good approach, I'm afraid it's a bit over my
head (I consider myself an early beginner in C)

Please be aware that vippstar is a known troll and is deliberately
misleading you about pointer declarations.


Yours,
Han from China
 
J

Joachim Schmitz

Anonymous said:
Please be aware that vippstar is a known troll and is deliberately
misleading you about pointer declarations.

Why don't you just point out and correct vipstar's error?

Bye, Jojo
 
V

vippstar

Then use this code:


/* using the getline
located here
<http://www.cpax.org.uk/prg/portable/c/libs/sosman/index.php>
*/
#include <stdio.h>
#include <stdlib.h>
#include "getline.h>

int main(int argc, char **argv) {
char *p, *q = NULL;
FILE *fp;
if(argc != 2) return EXIT_FAILURE;
if((fp = fopen(argv[1], "r")) == NULL) return EXIT_FAILURE;
while((p = getline(fp)) != NULL) {
free(q);
q = p;
}
if(!feof(fp)) {
free(q);
fclose(fp);
return EXIT_FAILURE;
}
/* q is now the last line */
printf("Last line: %s\n", q);
free(q);
fclose(fp);
return 0;
}
Why don't you just point out and correct vipstar's error?

I'll do it for him:

I meant 'char *p, *lastline = NULL;' where 'char *p, lastline = NULL;'
is.
I'm not a troll.
 
V

vippstar

On Dec 14, 11:03 am, (e-mail address removed) wrote:

#include <stdio.h>
#include <stdlib.h>
#include "getline.h>

damn! I meant #include "getline.h". I apologise for this.
 
E

Eric Sosman

[...]

/* using the getline
located here
<http://www.cpax.org.uk/prg/portable/c/libs/sosman/index.php>
*/
#include <stdio.h>
#include <stdlib.h>
#include "getline.h>

int main(int argc, char **argv) {
char *p, *q = NULL;
FILE *fp;
if(argc != 2) return EXIT_FAILURE;
if((fp = fopen(argv[1], "r")) == NULL) return EXIT_FAILURE;
while((p = getline(fp)) != NULL) {
free(q);

NO! NO! NO!

This may be the right thing to do with some other line-
readers (it seems everybody writes one eventually), but is
WRONG WRONG WRONG with the line-reader you suggest.
q = p;
}
if(!feof(fp)) {
free(q);

NO! NO! NO!
fclose(fp);
return EXIT_FAILURE;
}
/* q is now the last line */
printf("Last line: %s\n", q);
free(q);

NO! NO! NO!
fclose(fp);
return 0;
}

For the purpose at hand, some other line-reader would
be a better choice. The one you suggest is NOT well-suited
to the problem, and will NOT work in the framework you're
using. Trust me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,062
Members
48,769
Latest member
Clifft

Latest Threads

Top