trying to read a file

Y

yo_mismo

Hi everyone,

I'm trying to read the first line of a file this way:
....
....
....
....
new_line=0;
while((read=read(fd, &info, sizeof(info))) > 0 && !new_line){
if (strcmp(&info, "\n") != 0){
strcat(line,info);
}
else{
new_line=1;
}
}

But i can't find the end of the line and this code goes until the end of the
file instead the end of the line.
The other problem is that i get a segmentation fault in strcat(line, info),
i've declared the variables like this:

char *line="";
char info;

That's all, i hope someone can help me 'cause i can't find the errors.

Thanx.
 
D

Derk Gwen

# while((read=read(fd, &info, sizeof(info))) > 0 && !new_line){
# if (strcmp(&info, "\n") != 0){
# strcat(line,info);
# }
# else{
# new_line=1;
# }
# }
#
# But i can't find the end of the line and this code goes until the end of the
# file instead the end of the line.

Check the documentation for the function. Does it say it will add a zero
byte terminator? Not all functions do that.
strcpy(p,q)
adds a zero byte after copying strlen(q) bytes, but
memcpy(p,q,strlen(q))
does not add a zero byte.

# The other problem is that i get a segmentation fault in strcat(line, info),
# i've declared the variables like this:
#
# char *line="";
# char info;

You have to allocate sufficient room for the concatenation before calling
strcat; also on most system you cannot modify a literal string.
 
J

Jens.Toerring

yo_mismo said:
I'm trying to read the first line of a file this way:
char *line="";
char info;
...
...
...
new_line=0;
while((read=read(fd, &info, sizeof(info))) > 0 && !new_line){
if (strcmp(&info, "\n") != 0){
strcat(line,info);
}
else{
new_line=1;
}
}
But i can't find the end of the line and this code goes until the end of the
file instead the end of the line.

Your first problem is that this is at least partly off-topic in clc,
there's no standard C function called read(). Please ask in a group
that's concerned with your operating system. You probably would have
less problems if you would use a (standard C) function like fget()
instead. Just make sure that 'info' is an int or you won't be able
to detect the EOF (and, of course, use a stream pointer (FILE*)
instead of a file descriptor).
The other problem is that i get a segmentation fault in strcat(line, info),
i've declared the variables like this:

That's no surprise. 'line' is a char pointer, pointing to a string
that contains just the '\0' character. Now, the initialization makes
it point to a "literal string", i.e. a string that can't be changed.
Moreover, even if you would be allowed to change that string, you
still would have only room for a single char (and that's already
taken by the end-of-string delimiter '\0'), so you can't put any-
thing more into it. What you need is either a large enough array of
characters or a dynamically allocated buffer.

Second, you can't use strcmp() and strcat() here. They both work on
strings, i.e. arrays of chars that have a '\0' at the end of the
text they are supposed to contain. But 'info' is just a single char,
not an array of chars. If you haven't silenced your compiler it
should at least complain loudly about your call of strcat() since
the second argument isn't a char pointer but a char (it won't for
strcmp() because you pass it the address of the 'info' char, so it
can't determine that what you pass it won't be a string).

Regards, Jens
 
J

Jens.Toerring

.... You probably would have less problems if you would use a
(standard C) function like fget() instead.

Sorry, make that fgetc(), there's no function named fget()!

Regards, Jens
 
R

Régis Troadec

yo_mismo said:
Hi everyone,
Hi,


I'm trying to read the first line of a file this way:
...
...
...
...
new_line=0;
while((read=read(fd, &info, sizeof(info))) > 0 && !new_line){

There's no such function (read) in C. According to your requirements, I
would have used characters I/O functions like fgets or getc instead.
if (strcmp(&info, "\n") != 0){

info is a char, not a string. Functions of <string.h> work on strings (i.e
arrays of chars ended with the null terminating character \0) and look for
the null terminating character to process.
It would have worked for example in this case :
/*...*/
char info[2];
info[0] = '\n'; /*or else char..*/
info[1] = '\0';
/* strcmp shall return 0, the expression is evaluated to false*/
if (strcmp(info,"\n") != 0)
{
/*...*/
}
strcat(line,info);

Same remark as above. In addition to that, strcat requires sufficient
allocated space to store the result in line, but you defined line as a
string with only the null terminating character. line may also be read-only.
}
else{
new_line=1;
}
}

But i can't find the end of the line and this code goes until the end of the
file instead the end of the line.
The other problem is that i get a segmentation fault in strcat(line, info),
i've declared the variables like this:

char *line="";
char info;

That's all, i hope someone can help me 'cause i can't find the errors.

Here is a sample program which reads and displays the first line of a text
file passed in argument, using fgetc or fgets:

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

#define MAX_CHARS_PER_LINE 120

/* #define BASIC */

int main(int argc, char *argv[])
{
FILE * fp;
int c;
char linebuffer[MAX_CHARS_PER_LINE];

strcpy(linebuffer,"nothing");

if (argc < 2)
{
fprintf(stderr, "Not enough arguments\n");
fprintf(stderr, "Usage : readfl <file>\n"
"readfl displays the first line of the text file <file>\n");
exit(EXIT_FAILURE);
}

fp = fopen(argv[1], "r");

if (fp != NULL)
{

#ifdef BASIC

int cnt = 0;
while (((c = fgetc(fp)) != '\n' && c != EOF)
&& cnt < MAX_CHARS_PER_LINE-1)
{
linebuffer[cnt++] = c;
}
linebuffer[cnt] = '\0';

#else

fgets(linebuffer, MAX_CHARS_PER_LINE, fp);

#endif

puts(linebuffer);

fclose(fp);
exit(EXIT_SUCCESS);

}
else
{
fprintf(stderr, "Couldn't open the file to read\n");
exit(EXIT_FAILURE);
}

return 0;
}


HTH
Regis
 

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,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top