In comp.os.linux.development.apps, Paulo dos Santos
on Wed, 10 Jan 2001 11:48:10 GMT
>I am with a problem that I don't know the cause.
>I make three functions (main(), read_file(arg, arg), write_file(arg,
>The main() function call the write_file(). The structure of this last
>(function) is the following:
>write_file(char *arg1, char *arg2)
>int a=0, b=0;
>char *ab, *ac, *ad;
>ab = malloc(10);
>ac = malloc(10);
>ad = malloc(10);
Without knowing what these are for, this looks rather peculiar.
What you're instructing is the allocation of 10 byte for each
of ab, ac, and ad.
>all right. next I verify if this vars allocated. next I open a file in
>read mode - 'r' - (fopen)
Pedant point: "r". (Mind you, gcc/g++ will warn you.)
Quote:>and do a verification if occurs errors on
>this operation (to open). inside a loop (the control is done by a
>comparation strcmp(fgets(Arq), arg1)) I assign to var 'ab' the return
>of the fgets(Arq) function.
This is simply wrong. fgets() returns a FILE *, and requires
3 arguments. ('man fgets' for details on this function.)
fgets() will return either the third argument (the file pointer),
or NULL (indicating EOF or an I/O error).
strcmp(), by contrast, takes 2 arguments, both of which are
char * or const char * pointers. ('man strcmp'.)
You either want fgets(ab, 10, Arq) (which reads a single
newline-terminated('\n') line from a file, maximum of 9 characters
(+ 1 for the terminating NUL character ('\0')), or maybe
fread(ab, 1 , 10, Arq), which reads in exactly 10 bytes at a time,
no more, no less, until end of file is hit. Note that fread
will return an integer (the number of structures read -- in this
case, each "structure" is 1 byte in size), and will not terminate
the buffer with a NUL character; I wouldn't recommend using it
unless you know exactly what you're doing. ('man fread'.)
You can then validate that fgets() worked (it returns NULL on
end of file or I/O error), then you can compare ab to arg1.
Something like this would probably work:
while(fgets(ab, 10, Arq) != NULL)
/* they're equal */
although it depends on what you really want to do.
>I verify if is end-of-file (feof(Arq)). if true, is performed a
>verification a=0. if the condition was satisfied the file is blank (no
>records). if false (a!=0) is performed other verification b=0. if b
>equal 0 the argument arg1 doesn't exists on the opened file.
>case not be end-of-file, the var 'a' is incremented by 1 (a++) and the
>following declaration is executed:
>b = strcmp(fgets(Arq), arg1). the loop restart.
>the problem is on the comparation of the vars 'a' and 'b'. when these
>is compared with 0 (a==0 and b==0) the program aborts (Segmentation
>fault. Core dumped). but when these is compared with values greater
>than 0 (a>0 e b>0) the function normally continues.
>what is the restriction (or problem) in this case?
Well, there are a fair number of usage errors of fgets() and
strcmp() in the code snippets you have given us; until those
are fixed, you won't get very far. :-)
>note: I am using the GNU GCC compiler on a redhat linux 6 (2.2.14
>Thank you and excuse me for the spell errors.
>Sent via Deja.com
EAC code #191 4d:08h:48m actually running Linux.
Are you still here?