473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

trying to read a file

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.


Nov 14 '05 #1
4 2440
# 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.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
You hate people.
But I love gatherings. Isn't it ironic.
Nov 14 '05 #2
yo_mismo <qw*@rty.com> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3
Je***********@physik.fu-berlin.de wrote:
.... 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

--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4

"yo_mismo" <qw*@rty.com> a écrit dans le message de
news:hD******************@news.ono.com...
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

Thanx.

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: IronMonk | last post by:
Hi I am trying to edit a text file... the format of which is like #####abc.conf poll somethin proto pop3 via 127.9.02.1 user "me@me.com" pass "dell" is me.nutcase preconnect "abc abc abc...
5
by: jdh2358 | last post by:
I have a python file that is trying to read raw data from a raw partition on a dying dist, eg f = file('/dev/sda') f.seek(SOMEWHERE) s = f.read(SOMEBYTES) On some blocks, the read succeeds,...
3
by: Les Desser | last post by:
In A97 I am trying to determine some attributes of files in a folder - Date, time, file size and file name. To this end I have managed to create a batch file to do a Dir to a work file (and then...
4
by: Jeff Rodriguez | last post by:
Main just loops over this while it's not null. The segfault occurs at this line: *line = (char)ch; Also, please don't just fix the code. I would like to know why exactly this isn't working so I...
4
by: yo_mismo | last post by:
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") !=...
3
by: jdjohns74 | last post by:
I've never written a Python program before and I'm trying to read a config file with file path/names (eg. c:\\python24\\*.dll, ... *.exe) to create an ouput file of filename + md5 values. I'm...
1
by: James Johnston | last post by:
I've never written a Python program before and I'm trying to read a config file with file path/names (eg. c:\\python24\\*.dll, ... *.exe) to create an output file of filename + md5 values. I'm...
1
by: jonathan184 | last post by:
trying to rename filenames and extensions then add a header in line1 of each file if the header existed in line 1 to go to the next file. but i am getting error explciti errors Here is my...
7
by: JoeC | last post by:
I am trying to create a windows program that reads binary graphics as a resource. This has nothing to do with win32 but conversion of data with memcpy. graphic::graphic(UINT uiResID, HINSTANCE...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.