How can i read lines of a file and place each line read in an array?
for exemple;
array[0]=line1
array[1]=line2
...
|
|
0
|
|
|
|
Reply
|
chitlesh1 (22)
|
5/30/2005 4:50:49 PM |
|
On Mon, 30 May 2005 16:50:49 +0000 (UTC),
chitlesh@gmail-dot-com.no-spam.invalid (Clunixchit) wrote:
>How can i read lines of a file and place each line read in an array?
>for exemple;
>array[0]=line1
>array[1]=line2
Your use of the word line implies a text file. fgets() is frequently
the tool of choice.
If the file is not a text file, then fread() would probably be better.
<<Remove the del for email>>
|
|
0
|
|
|
|
Reply
|
schwarzb (662)
|
5/30/2005 10:56:32 PM
|
|
Thanks ive used fgets
|
|
0
|
|
|
|
Reply
|
chitlesh1 (22)
|
6/1/2005 12:49:01 PM
|
|
im having a *** glibc detected *** corrupted double-linked list:
0x0804b error.
here is my code :
typedef struct new_phrase {
int N; // number of words
char **T; // words stored in an array
} new_phrase;
new_phrase file_array (new_phrase phrase) {
phrase.N=0;
FILE *stream;
char tmp[LINE_LENGTH];
/* Open the file. If NULL is returned there was an error */
if ( !(stream = fopen( MYAIRC , "r"
))) {
perror("fopen");
_exit(EXIT_FAILURE);
}
(void) fseek(stream,0,SEEK_SET);
if ( !(phrase.T = malloc ( sizeof (phrase.T)
))) {
perror("malloc");
_exit(EXIT_FAILURE);
}
// creating array > phrase.T
while ( fgets ( tmp , LINE_LENGTH-1 , stream ) != NULL
) {
size_t l = strlen(tmp);
if (l > 0 && tmp[l-1] == '\n')
tmp[l-1] = '\0';
if ( !(phrase.T[phrase.N] = malloc ( sizeof
(phrase.T[phrase.N]) ))) {
perror("malloc");
_exit(EXIT_FAILURE);
}
phrase.T[phrase.N]=tmp;
printf("phrase.T %s
%d\n",phrase.T[phrase.N],phrase.N);
phrase.N++;
}
fclose (stream);
if ( phrase.N == 0 ) {
printf("%s is empty\n",MYAIRC);
_exit(EXIT_FAILURE);
}
return phrase;
}
ive used the fgets mentioned above.
this code allows to fill the array but return the error
*** glibc detected *** corrupted double-linked list: 0x0804b168 ***
any suggestions? or improvements?
>
> phrase.T xlogo 0
> phrase.T ls -i 1
> phrase.T ls -l 2
> phrase.T xload 3
> *** glibc detected *** corrupted double-linked list: 0x0804b168 ***
>
|
|
0
|
|
|
|
Reply
|
chitlesh1 (22)
|
6/18/2005 12:58:09 PM
|
|
In article <d915p1$l03$1@domitilla.aioe.org>
Clunixchit <chitlesh@gmail-dot-com.no-spam.invalid> wrote:
>here is my code :
[snippage - some slight editing for space]
> if ( !(phrase.T[phrase.N] = malloc(sizeof (phrase.T[phrase.N]) ))) {
This line is obviously wrong.
A call to malloc() should always have the general form:
p = malloc(N * sizeof *p)
or:
p = malloc(sizeof *p) /* i.e., N == 1 */
or, similarly:
*pp = malloc(N * sizeof **pp)
but this one has the form:
p = malloc(sizeof p)
The unary "*" operator is missing: there should be one more "*" on
the right side of the "=" sign than there is on the left.
The line above should read more like:
if ((phrase.T[phrase.N] = malloc(sizeof *phrase.T[phrase.N])) == NULL) {
for instance.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40�39.22'N, 111�50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
|
|
0
|
|
|
|
Reply
|
nospam252 (1722)
|
6/18/2005 4:57:50 PM
|
|
> *** glibc detected *** corrupted double-linked list: 0x0804b168 ***
nope it didnt solve the problem.
The Anjuta program giving me:
> Program has been terminated receiving signal 6 (Aborted)
in the end
|
|
0
|
|
|
|
Reply
|
chitlesh1 (22)
|
6/18/2005 5:47:11 PM
|
|
On Sat, 18 Jun 2005 12:58:09 +0000 (UTC),
chitlesh@gmail-dot-com.no-spam.invalid (Clunixchit) wrote:
>im having a *** glibc detected *** corrupted double-linked list:
>0x0804b error.
>here is my code :
>
>typedef struct new_phrase {
> int N; // number of words
> char **T; // words stored in an array
>} new_phrase;
>
>new_phrase file_array (new_phrase phrase) {
> phrase.N=0;
> FILE *stream;
> char tmp[LINE_LENGTH];
>
> /* Open the file. If NULL is returned there was an error */
> if ( !(stream = fopen( MYAIRC , "r"))) {
> perror("fopen");
> _exit(EXIT_FAILURE);
What is _exit? Is there some reason the standard function won't work
for you so others could test your code?
> }
> (void) fseek(stream,0,SEEK_SET);
The cast does nothing but clutter up your code.
>
> if ( !(phrase.T = malloc ( sizeof (phrase.T)))) {
You have attempted to allocate enough space for a char**. However,
phrase.T must point to an area with enough space for a char*. It is
entirely possible for the latter to be larger than the former. You
probably want malloc(sizeof *phrase.T).
> perror("malloc");
> _exit(EXIT_FAILURE);
> }
>
> // creating array > phrase.T
> while ( fgets ( tmp , LINE_LENGTH-1 , stream ) != NULL
fgets() already subtracts one for you so there is no need for you to
subtract another one.
>) {
> size_t l = strlen(tmp);
> if (l > 0 && tmp[l-1] == '\n')
> tmp[l-1] = '\0';
> if ( !(phrase.T[phrase.N] = malloc ( sizeof
>(phrase.T[phrase.N]) ))) {
This is wrong for a more complex reason. You really don't care about
the size of phrase.T[phrase.N]. That is simply a char* whose size
never changes. You want to allocate enough space to hold the string.
l+1 will always be sufficient though if there was a \n in the string
you removed it and l would be sufficient. Your code could read
malloc(l+1).
If sizeof(char*) > sizeof(char**), then this invokes undefined
behavior by attempting to stuff a wider value into a narrower
allocation.
> perror("malloc");
> _exit(EXIT_FAILURE);
> }
> phrase.T[phrase.N]=tmp;
This throws away the address of the area you just allocated, creating
a memory leak. I think you intended to copy the string in tmp to the
newly allocated area. For that you need something akin to strcpy().
> printf("phrase.T %s
>%d\n",phrase.T[phrase.N],phrase.N);
> phrase.N++;
> }
>
> fclose (stream);
>
> if ( phrase.N == 0 ) {
> printf("%s is empty\n",MYAIRC);
> _exit(EXIT_FAILURE);
> }
> return phrase;
Your structure is very small (two simple items) so this is not a big
deal. But most C code would use a pointer to struct to avoid the
overhead of passing a large struct back and forth.
>}
>
>ive used the fgets mentioned above.
>this code allows to fill the array but return the error
What array? The only array in your code is tmp. And it ceases to
exist as soon as you exit the function.
Furthermore, this function will only allocate one value in the area
pointed to by phrase.T. You set phrase.N to 0 on entry. No matter
what the struct contains from the caller, you force it to be "empty".
>*** glibc detected *** corrupted double-linked list: 0x0804b168 ***
Have you run it through a debugger? On which statement does this
error occur? Since you have no linked list in your code, this must
have something to do with the run time library, most likely memory
allocation. Fix the obvious problems and try again.
>
>any suggestions? or improvements?
>>
>> phrase.T xlogo 0
>> phrase.T ls -i 1
>> phrase.T ls -l 2
>> phrase.T xload 3
>> *** glibc detected *** corrupted double-linked list: 0x0804b168 ***
It would have helped if you had shown us how you call the function.
<<Remove the del for email>>
|
|
0
|
|
|
|
Reply
|
schwarzb (662)
|
6/18/2005 6:50:56 PM
|
|
> It would have helped if you had shown us how you call the function.
here it is:
new_phrase phrase;
//initialise the array in new_phrase
phrase = file_array(phrase);
i will correct with respect to your suggestion and will post
accordingingly
|
|
0
|
|
|
|
Reply
|
chitlesh1 (22)
|
6/18/2005 8:33:46 PM
|
|
i can't find whats going wrong with it
here is the full source code:
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#define LINE_LENGTH 20
#define MYAIRC "myAIc"
typedef struct new_phrase { // if new_phrase is not written here,
it will be an anonymous structure
int N; // number of words
char **T; // words stored in an array
} new_phrase;
/* initialise my structure by first loading MYAIRC into a
bidimensional array*/
new_phrase * file_array (new_phrase *phrase) {
(*phrase).N=0;
char tmp[LINE_LENGTH];
FILE *stream = fopen( MYAIRC , "r" );
if ( !stream ) {
perror("fopen");
_exit(EXIT_FAILURE);
}
fseek(stream,0,SEEK_SET);
if ( !((*phrase).T = malloc ( sizeof
*((*phrase).T) ))) {
perror("malloc");
_exit(EXIT_FAILURE);
}
while ( fgets( tmp , LINE_LENGTH , stream ) != NULL )
{
size_t l = strlen(tmp);
if (l > 0 && tmp[l-1] == '\n')
tmp[l-1] = '\0';
if ( !((*phrase).T[(*phrase).N] =
malloc ( strlen(tmp) ))) {
perror("malloc");
_exit(EXIT_FAILURE);
}
strcpy((*phrase).T[(*phrase).N],tmp);
printf("phrase.T %s
%d\n",(*phrase).T[(*phrase).N],(*phrase).N);
(*phrase).N++;
}
fclose (stream);
if ( (*phrase).N == 0 ) {
printf("%s is empty\n",MYAIRC);
_exit(EXIT_FAILURE);
}
return phrase;
}
int main ( void ) {
new_phrase *phrase = malloc ( sizeof(*phrase));
//initialise the structure
new_phrase *phrase1 = file_array(phrase);
return 0;
}
|
|
0
|
|
|
|
Reply
|
chitlesh1 (22)
|
6/18/2005 10:47:05 PM
|
|
Clunixchit wrote:
> i can't find whats going wrong with it
> here is the full source code:
Just in case anyone cares to bother, here is a legal standard C version
of your code. This code compiles and with a copy of itself named
"myAIc" runs without error.
#if 0
/* mha: non-standard headers placed in this commented-out section */
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#endif
/* mha: Further general notes -- (1) The C++-style comments have been
converted. Your example code, with comments broken across lines, is
exactly why C++-style comments should not be used in usenet
postings. (2) The non-standard _exit() calls have been replaced
with exit(). Any time you need to #include <unistd.h>, you know
your code is not standard C. (3) The broken printf() specifier
string has been spliced together. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_LENGTH 20
#define MYAIRC "myAIc"
typedef struct new_phrase
{ /* if new_phrase is not written here,
it will be an anonymous structure */
int N; /* number of words */
char **T; /* words stored in an array */
} new_phrase;
/* initialise my structure by first loading MYAIRC into a
bidimensional array*/
new_phrase *file_array(new_phrase * phrase)
{
(*phrase).N = 0;
char tmp[LINE_LENGTH];
FILE *stream = fopen(MYAIRC, "r");
if (!stream) {
perror("fopen");
exit(EXIT_FAILURE);
}
fseek(stream, 0, SEEK_SET);
if (!((*phrase).T = malloc(sizeof *((*phrase).T)))) {
perror("malloc");
exit(EXIT_FAILURE);
}
while (fgets(tmp, LINE_LENGTH, stream) != NULL) {
size_t l = strlen(tmp);
if (l > 0 && tmp[l - 1] == '\n')
tmp[l - 1] = '\0';
if (!((*phrase).T[(*phrase).N] = malloc(strlen(tmp)))) {
perror("malloc");
exit(EXIT_FAILURE);
}
strcpy((*phrase).T[(*phrase).N], tmp);
printf("phrase.T %s %d\n", (*phrase).T[(*phrase).N],
(*phrase).N);
(*phrase).N++;
}
fclose(stream);
if ((*phrase).N == 0) {
printf("%s is empty\n", MYAIRC);
exit(EXIT_FAILURE);
}
return phrase;
}
int main(void)
{
new_phrase *phrase = malloc(sizeof(*phrase));
/* initialise the structure */
new_phrase *phrase1 = file_array(phrase);
return 0;
}
|
|
0
|
|
|
|
Reply
|
mambuhl (2201)
|
6/19/2005 12:44:44 AM
|
|
> Martin Ambuhlwrote:
Clunixchit wrote:
> i can't find whats going wrong with it
> here is the full source code:
>
Just in case anyone cares to bother, here is a legal standard C
version
of your code. This code compiles and with a copy of itself named
"myAIc" runs without error.
#if 0
/* mha: non-standard headers placed in this commented-out section
*/
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#endif
/* mha: Further general notes -- (1) The C++-style comments have
been
converted. Your example code, with comments broken across lines,
is
exactly why C++-style comments should not be used in usenet
postings. (2) The non-standard _exit() calls have been replaced
with exit(). Any time you need to #include <unistd.h>, you
know
your code is not standard C. (3) The broken printf() specifier
string has been spliced together. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_LENGTH 20
#define MYAIRC "myAIc"
typedef struct new_phrase
{ /* if new_phrase is not written here,
it will be an anonymous structure
*/
int N; /* number of words */
char **T; /* words stored in an array */
} new_phrase;
/* initialise my structure by first loading MYAIRC into a
bidimensional array*/
new_phrase *file_array(new_phrase * phrase)
{
(*phrase).N = 0;
.........................................
return 0;
}[/quote:78b2fb99c6]
have you tested the code at your place? coz im still have the same
glib error
|
|
0
|
|
|
|
Reply
|
chitlesh1 (22)
|
6/19/2005 11:47:10 AM
|
|
On Sat, 18 Jun 2005 16:57:50 +0000, Chris Torek wrote:
> In article <d915p1$l03$1@domitilla.aioe.org>
> Clunixchit <chitlesh@gmail-dot-com.no-spam.invalid> wrote:
>>here is my code :
>
> [snippage - some slight editing for space]
>
>> if ( !(phrase.T[phrase.N] = malloc(sizeof (phrase.T[phrase.N]) ))) {
>
> This line is obviously wrong.
>
> A call to malloc() should always have the general form:
>
> p = malloc(N * sizeof *p)
Except when p is void *. :-)
Lawrence
|
|
0
|
|
|
|
Reply
|
lknews (877)
|
6/19/2005 12:38:33 PM
|
|
Clunixchit wrote:
>>Martin Ambuhlwrote:
[...]
> This code compiles and with a copy of itself named
> "myAIc" runs without error.
> have you tested the code at your place? coz im still have the same
> glib error
Can you read? How the hell would I know that it 'compiles and with a
copy of itself named "myAIc" runs without error' if I hadn't tested it?
|
|
0
|
|
|
|
Reply
|
mambuhl (2201)
|
6/19/2005 3:30:19 PM
|
|
Martin Ambuhl wrote:
> [...]
> if (!((*phrase).T[(*phrase).N] = malloc(strlen(tmp)))) {
> [...]
> strcpy((*phrase).T[(*phrase).N], tmp);
Off-by-one error.
--
Eric Sosman
esosman@acm-dot-org.invalid
|
|
0
|
|
|
|
Reply
|
esosman (1335)
|
6/19/2005 3:53:05 PM
|
|
Eric Sosman wrote:
> Martin Ambuhl wrote:
>
>> [...]
>> if (!((*phrase).T[(*phrase).N] = malloc(strlen(tmp)))) {
>> [...]
>> strcpy((*phrase).T[(*phrase).N], tmp);
>
>
> Off-by-one error.
>
Well spotted. In case chitlesh@gmail-dot-com.no-spam.invalid
(Clunixchit), who is responsible for the logic (I claimed only to make a
legal compilable version available to anyone who cared), doesn't
understand the rather terse comment by Eric:
to copy the string tmp, the target must have a size of strlen(tmp)+1
or greater. The terminating '\0' which is necessary is not counted in
the value returned by strlen().
|
|
0
|
|
|
|
Reply
|
mambuhl (2201)
|
6/19/2005 4:20:43 PM
|
|
I got my code working only if i create another "while".
As u can see i have made modifications with respect to your
suggestions. Ill be happy to receive more :)
concerning the non-standard libraries, this is because im using fork
and the exec family.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILLED 1
#define EMPTY 0
#define LINE_LENGTH 20
#define MYAIRC "myAIrc"
typedef struct new_phrase {
int N;
char **T;
} new_phrase;
/* initialise my structure by first loading MYAIRC into a
bidimensional array*/
new_phrase * file_array () {
int i=0 , j=0;
char tmp[LINE_LENGTH][LINE_LENGTH];
new_phrase *phrase = malloc (sizeof *phrase);//8
(*phrase).N = EMPTY;
FILE *stream = fopen( MYAIRC , "r" );
if ( !stream ) {
perror("fopen");
exit(EXIT_FAILURE);
}
fseek(stream,0,SEEK_SET);
(*phrase).T = malloc ( sizeof
(*(*phrase).T) );
while ( fgets ( tmp[i] , LINE_LENGTH , stream )
) {
(*phrase).N=FILLED;
size_t l = strlen(tmp[i]);
if (l > 0 && tmp[i][l-1] ==
'\n')
tmp[i][l-1] = '\0';
i++;
}
fclose (stream);
while ( j < i ) {
(*phrase).T[j] = malloc (
strlen(tmp[j]));
strcpy((*phrase).T[j],tmp[j]);
printf("phrase.T %s
%d\n",(*phrase).T[j],j);
j++;
}
return phrase;
}
int main ( ) {
new_phrase *phrase = malloc(sizeof(*phrase));
phrase = file_array();
return 0;
}
|
|
0
|
|
|
|
Reply
|
chitlesh1 (22)
|
6/20/2005 12:00:05 PM
|
|
Barry Schwarz wrote:
> On Sat, 18 Jun 2005 12:58:09 +0000 (UTC),
> chitlesh@gmail-dot-com.no-spam.invalid (Clunixchit) wrote:
>
> >im having a *** glibc detected *** corrupted double-linked list:
> >0x0804b error.
> >here is my code :
> >
> >typedef struct new_phrase {
> > int N; // number of words
> > char **T; // words stored in an array
> >} new_phrase;
> >
> >new_phrase file_array (new_phrase phrase) {
> > phrase.N=0;
> > FILE *stream;
> > char tmp[LINE_LENGTH];
> >
> > /* Open the file. If NULL is returned there was an error */
> > if ( !(stream = fopen( MYAIRC , "r"))) {
> > perror("fopen");
> > _exit(EXIT_FAILURE);
>
> What is _exit? Is there some reason the standard function won't work
> for you so others could test your code?
>
I had always thought _exit was C. Having looked, I can see that _Exit
is
C99, and means about the same thing as the pretty widely available (but
OT here) Posix/BSD/etc _exit. Means quit the program, do not pass go,
do not invoke atexit registered functions, don't invoke signal
handlers, maybe skip some other cleanup. Not clear why the OP is using
it, seems to be designed
for more dire situations than failing to open a file (not sure what,
when might you need to call this, some sort of error or signal
handling?)
-David
|
|
0
|
|
|
|
Reply
|
lndresnick (326)
|
6/20/2005 12:16:34 PM
|
|
On Mon, 20 Jun 2005 12:00:05 +0000 (UTC), in comp.lang.c ,
chitlesh@gmail-dot-com.no-spam.invalid (Clunixchit) wrote:
> new_phrase *phrase = malloc (sizeof *phrase);//8
> (*phrase).N = EMPTY;
This is an obscure way to say
phrase->N = EMPTY;
I'd suggest you use the latter, since its the normal means of doing it
in C.
> (*phrase).T = malloc ( sizeof (*(*phrase).T) );
and this is a very odd way of saying
phrase->T = malloc( sizeof (*(phrase->T)));
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
|
|
0
|
|
|
|
Reply
|
markmcintyre (4547)
|
6/20/2005 1:12:20 PM
|
|
On Mon, 20 Jun 2005 12:00:05 +0000, Clunixchit wrote:
> I got my code working only if i create another "while".
> As u can see i have made modifications with respect to your
> suggestions. Ill be happy to receive more :)
> concerning the non-standard libraries, this is because im using fork
> and the exec family.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> #define FILLED 1
> #define EMPTY 0
> #define LINE_LENGTH 20
> #define MYAIRC "myAIrc"
>
> typedef struct new_phrase {
> int N;
> char **T;
> } new_phrase;
>
> /* initialise my structure by first loading MYAIRC into a
> bidimensional array*/
> new_phrase * file_array () {
> int i=0 , j=0;
> char tmp[LINE_LENGTH][LINE_LENGTH];
LINE_LENGTH seems an odd value to use for the first dimension.
> new_phrase *phrase = malloc (sizeof *phrase);//8
Bad comment. It doesn't add any useful information and is wrong in
general. You should test the return value of malloc() for failure.
> (*phrase).N = EMPTY;
A slighty obfiscated way of writing
phrase->N = EMPTY;
> FILE *stream = fopen( MYAIRC , "r" );
> if ( !stream ) {
> perror("fopen");
> exit(EXIT_FAILURE);
> }
> fseek(stream,0,SEEK_SET);
A file opened in non-append more will initially be positioned at the
start of the file so this seek has no effect.
> (*phrase).T = malloc ( sizeof
> (*(*phrase).T) );
You might find this easier to read as
phrase->T = malloc(sizeof *phrase->T);
Again you should test the return value. Note that you are allocating
space for a single pointer to char.
> while ( fgets ( tmp[i] , LINE_LENGTH , stream )
> ) {
> (*phrase).N=FILLED;
> size_t l = strlen(tmp[i]);
> if (l > 0 && tmp[i][l-1] ==
> '\n')
> tmp[i][l-1] = '\0';
> i++;
> }
> fclose (stream);
>
> while ( j < i ) {
> (*phrase).T[j] = malloc (
> strlen(tmp[j]));
Since you allocated only a single pointer to char this will produce
undefined behaviour when j != 0.
> strcpy((*phrase).T[j],tmp[j]);
This will additionally produce undefined behaviour because you didn't allocate
space for the terminating null character of the string.
> printf("phrase.T %s
> %d\n",(*phrase).T[j],j);
> j++;
> }
> return phrase;
> }
> int main ( ) {
>
> new_phrase *phrase = malloc(sizeof(*phrase));
>
> phrase = file_array();
You have a memory leak here. You allocate an object and make phrase a
pointer to it. Then you overwrite phrase and you no longer have a way
to access the object you allocated.
> return 0;
> }
Lawrence
|
|
0
|
|
|
|
Reply
|
lknews (877)
|
6/20/2005 6:16:27 PM
|
|
> Lawrence Kirbywrote:
On Mon, 20 Jun 2005 12:00:05 +0000, Clunixchit wrote:
>
> new_phrase *phrase = malloc (sizeof *phrase);//8
>
Bad comment. It doesn't add any useful information and is wrong in
general. You should test the return value of malloc() for failure.
Lawrence[/quote:58521360ca]
can you tell me how far is that true, the post at
http://forums.fedoraforum.org/forum/showthread.php?t=60180
last time someone in a forum told not to do that any more
|
|
0
|
|
|
|
Reply
|
chitlesh1 (22)
|
6/20/2005 11:27:13 PM
|
|
does
(*phrase).T = malloc ( sizeof
(*(*phrase).T) );
means
[code:1:f9ca2a067e]
(*phrase).T[b][0][/b] = malloc (
sizeof
(*(*phrase).T[b][0][/b])
);[/code:1:f9ca2a067e]
?
|
|
0
|
|
|
|
Reply
|
chitlesh1 (22)
|
6/20/2005 11:27:14 PM
|
|
On 20 Jun 2005 05:16:34 -0700, "David Resnick" <lndresnick@gmail.com>
wrote:
>
>
>Barry Schwarz wrote:
>> On Sat, 18 Jun 2005 12:58:09 +0000 (UTC),
>> chitlesh@gmail-dot-com.no-spam.invalid (Clunixchit) wrote:
>>
snip
>> >new_phrase file_array (new_phrase phrase) {
>> > phrase.N=0;
>> > FILE *stream;
>> > char tmp[LINE_LENGTH];
>> >
>> > /* Open the file. If NULL is returned there was an error */
>> > if ( !(stream = fopen( MYAIRC , "r"))) {
>> > perror("fopen");
>> > _exit(EXIT_FAILURE);
>>
>> What is _exit? Is there some reason the standard function won't work
>> for you so others could test your code?
>>
>
>I had always thought _exit was C. Having looked, I can see that _Exit
>is
>C99, and means about the same thing as the pretty widely available (but
>OT here) Posix/BSD/etc _exit. Means quit the program, do not pass go,
There is no _exit and no _Exit defined in n869 and I doubt if it was
added to the standard at the last minute. The standard exit function
is the one declared in stdlib.h as
void exit(int status);
>do not invoke atexit registered functions, don't invoke signal
>handlers, maybe skip some other cleanup. Not clear why the OP is using
>it, seems to be designed
>for more dire situations than failing to open a file (not sure what,
>when might you need to call this, some sort of error or signal
>handling?)
>
>-David
<<Remove the del for email>>
|
|
0
|
|
|
|
Reply
|
schwarzb (662)
|
6/21/2005 6:34:36 AM
|
|
Barry Schwarz <schwarzb@deloz.net> wrote:
> On 20 Jun 2005 05:16:34 -0700, "David Resnick" <lndresnick@gmail.com>
> wrote:
>
> >I had always thought _exit was C. Having looked, I can see that _Exit is
> >C99, and means about the same thing as the pretty widely available (but
> >OT here) Posix/BSD/etc _exit. Means quit the program, do not pass go,
>
> There is no _exit and no _Exit defined in n869 and I doubt if it was
> added to the standard at the last minute.
Ah, but it was!
If you want an updated Draft Standard, there's an n1124.pdf, of, AFAICT,
the final C99 Standard with a (final?) draft of TC2 worked in, at
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>.
Richard
|
|
0
|
|
|
|
Reply
|
rlb (4118)
|
6/21/2005 7:50:58 AM
|
|
Barry Schwarz <schwarzb@deloz.net> writes:
> On 20 Jun 2005 05:16:34 -0700, "David Resnick" <lndresnick@gmail.com>
> wrote:
>>I had always thought _exit was C. Having looked, I can see that
>>_Exit is C99, and means about the same thing as the pretty widely
>>available (but OT here) Posix/BSD/etc _exit. Means quit the
>>program, do not pass go,
>
> There is no _exit and no _Exit defined in n869 and I doubt if it was
> added to the standard at the last minute. The standard exit function
> is the one declared in stdlib.h as
> void exit(int status);
Bzzzt.
There is no _Exit in C90 or in n869, but it *was* added to the
standard at the last minute. C99 7.20.4.4. It terminates the program
without calling calling any function registered with atexit; whether
open streams and temporary files are cleaned up is
implementation-defined.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
|
|
0
|
|
|
|
Reply
|
kst-u (21460)
|
6/21/2005 8:04:00 AM
|
|
On Mon, 20 Jun 2005 23:27:14 +0000, Clunixchit wrote:
> does
>
> (*phrase).T = malloc ( sizeof
> (*(*phrase).T) );
>
> means
> [code:1:f9ca2a067e]
> (*phrase).T[b][0][/b] = malloc (
> sizeof
> (*(*phrase).T[b][0][/b])
> );[/code:1:f9ca2a067e]
Usenet is a plain text medium, turn off whatever is genarating the
formatting codes.
But if I read that correctly you're asking whether a pointer is the same
as the thing it points at. The answer to that is no.
Lawrence
|
|
0
|
|
|
|
Reply
|
lknews (877)
|
6/21/2005 10:00:37 AM
|
|
On Mon, 20 Jun 2005 23:27:13 +0000, Clunixchit wrote:
>> Lawrence Kirbywrote:
> On Mon, 20 Jun 2005 12:00:05 +0000, Clunixchit wrote:
>>
>> new_phrase *phrase = malloc (sizeof *phrase);//8
>>
> Bad comment. It doesn't add any useful information and is wrong in
> general. You should test the return value of malloc() for failure.
>
> Lawrence[/quote:58521360ca]
> can you tell me how far is that true, the post at
> http://forums.fedoraforum.org/forum/showthread.php?t=60180
>
> last time someone in a forum told not to do that any more
That post says that you can't depend on the value of errno after
malloc() fails. That's true but is a different issue. To test whether
malloc() has failed you check whether the return value is null. errno
doesn't tell you that something has failed (there are a couple of cases
where it does), the intent is that when defined it provides information
about why it has failed.
Lawrence
|
|
0
|
|
|
|
Reply
|
lknews (877)
|
6/21/2005 10:34:16 AM
|
|
chitlesh@gmail-dot-com.no-spam.invalid (Clunixchit) writes:
>> Lawrence Kirbywrote:
> On Mon, 20 Jun 2005 12:00:05 +0000, Clunixchit wrote:
>>
>> new_phrase *phrase = malloc (sizeof *phrase);//8
>>
> Bad comment. It doesn't add any useful information and is wrong in
> general. You should test the return value of malloc() for failure.
>
> Lawrence[/quote:58521360ca]
> can you tell me how far is that true, the post at
> http://forums.fedoraforum.org/forum/showthread.php?t=60180
>
> last time someone in a forum told not to do that any more
Please don't use your non-standard "[/quote:...]" formatting codes.
To quote text from a previous article, prefix each quoted line with
"> " (as you can see from 99% of the articles posted here).
Your Usenet client should be able to do this for you.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
|
|
0
|
|
|
|
Reply
|
kst-u (21460)
|
6/22/2005 12:41:38 AM
|
|
> Keith Thompsonwrote:
chitlesh@gmail-dot-com.no-spam.invalid (Clunixchit) writes:
> Lawrence Kirbywrote:
> On Mon, 20 Jun 2005 12:00:05 +0000, Clunixchit wrote:
>
> new_phrase *phrase = malloc (sizeof *phrase);//8
>
> Bad comment. It doesn't add any useful information and is wrong in
> general. You should test the return value of malloc() for failure.
>
> Lawrence
can you tell me how far is that true, the post at
http://forums.fedoraforum.org/forum/showthread.php?t=60180
last time someone in a forum told not to do that any more
[/quote:0a154a247e]
Please don't use your non-standard "[/quote:...]" formatting codes.
To quote text from a previous article, prefix each quoted line with
"> " (as you can see from 99% of the articles posted here).
Your Usenet client should be able to do this for you.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org
<http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*>
<http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do
this.[/quote:0a154a247e]
but im not using usenet !!
im on nixdoc.net
|
|
0
|
|
|
|
Reply
|
chitlesh1 (22)
|
6/22/2005 7:45:35 PM
|
|
chitlesh@gmail-dot-com.no-spam.invalid (Clunixchit) writes:
>> Keith Thompsonwrote:
[...]
> Please don't use your non-standard "[/quote:...]" formatting codes.
> To quote text from a previous article, prefix each quoted line with
> "> " (as you can see from 99% of the articles posted here).
>
> Your Usenet client should be able to do this for you.
[...]
> but im not using usenet !!
> im on nixdoc.net
Yes, you are. comp.lang.c is a Usenet newsgroup. I don't know what
interface you're using, but you are unquestionably using Usenet,
either directly or indirectly.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
|
|
0
|
|
|
|
Reply
|
kst-u (21460)
|
6/23/2005 12:36:04 PM
|
|
On Wed, 22 Jun 2005 19:45:35 +0000 (UTC), in comp.lang.c ,
chitlesh@gmail-dot-com.no-spam.invalid (Clunixchit) wrote:
>but im not using usenet !!
yes, you are. Comp.lang.c is a usenet group, and no matter what ISP
you're with, if you're posting articles to news groups, you're in
usenet.
>im on nixdoc.net
thats your isp.
By the way, your newsreader is severely broken - it also failed to
snip off Keith's signature. I've never heard of newsSync but you need
to get someone who knows about it, to set it up properly.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
|
|
0
|
|
|
|
Reply
|
markmcintyre (4547)
|
6/23/2005 10:58:03 PM
|
|
On Thu, 23 Jun 2005 23:58:03 +0100, Mark McIntyre
<markmcintyre@spamcop.net> wrote:
>On Wed, 22 Jun 2005 19:45:35 +0000 (UTC), in comp.lang.c ,
>chitlesh@gmail-dot-com.no-spam.invalid (Clunixchit) wrote:
>
>>but im not using usenet !!
>
>yes, you are. Comp.lang.c is a usenet group, and no matter what ISP
>you're with, if you're posting articles to news groups, you're in
>usenet.
>
Not necessarily. There are publically accessable newsgroups that
are not part of Usenet, such as the fora at newsgroups.borland.com
Oz
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
|
|
0
|
|
|
|
Reply
|
ozbear1 (126)
|
6/24/2005 8:29:33 AM
|
|
ozbear wrote:
> <markmcintyre@spamcop.net> wrote:
>> chitlesh@gmail-dot-com.no-spam.invalid (Clunixchit) wrote:
>>
>>> but im not using usenet !!
>>
>> yes, you are. Comp.lang.c is a usenet group, and no matter what
>> ISP you're with, if you're posting articles to news groups,
>> you're in usenet.
>
> Not necessarily. There are publically accessable newsgroups that
> are not part of Usenet, such as the fora at newsgroups.borland.com
Please don't confuse the newbie with nits. He is using c.l.c,
which is very definitely a part of usenet.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
6/24/2005 3:48:41 PM
|
|
On Fri, 24 Jun 2005 15:48:41 GMT, CBFalconer <cbfalconer@yahoo.com>
wrote:
>ozbear wrote:
>> <markmcintyre@spamcop.net> wrote:
>>> chitlesh@gmail-dot-com.no-spam.invalid (Clunixchit) wrote:
>>>
>>>> but im not using usenet !!
>>>
>>> yes, you are. Comp.lang.c is a usenet group, and no matter what
>>> ISP you're with, if you're posting articles to news groups,
>>> you're in usenet.
>>
>> Not necessarily. There are publically accessable newsgroups that
>> are not part of Usenet, such as the fora at newsgroups.borland.com
>
>Please don't confuse the newbie with nits. He is using c.l.c,
>which is very definitely a part of usenet.
>
Actually it is a rather large "nit" that even newbies should learn.
Not all "news servers" are part of Usenet and it can be quite
confusing to noobs that their posts never turn up on the "real"
fora servers because those servers don't take inbound Usenet
feeds but are just leeched from. Although not the case here
it is something that confuses people.
Oz
--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
|
|
0
|
|
|
|
Reply
|
ozbear1 (126)
|
6/24/2005 11:34:10 PM
|
|
|
32 Replies
39 Views
(page loaded in 0.36 seconds)
Similiar Articles: I want to read only a few lines from multiple text files - comp ...Suppose I have multiple text files in a directory, and I want to read only a few lines from each and then skip the rest of the text file and move on... Shortest way to read all lines (one by one) from a text file ...Shortest way to read all lines (one by one) from a text file ..... to read all lines (one by one) from a text file ... Multiple ... to read old PDF files - comp.text.pdf ... Reading TXT file into MSAccess using Line Input? - comp.databases ...I want to read only a few lines from multiple text files - comp ... Reading TXT file into MSAccess using Line Input? - comp.databases ... I need to read txt files into ... Reading a Line from a file - comp.soft-sys.matlabReading a data file with the command 'load' - comp.soft-sys.math ... load multiple files - comp.soft-sys.matlab Reading a Line from a file - comp.soft-sys.matlab Well, the ... How to read files from multiple folders - comp.soft-sys.matlab ...Suppose I have multiple text files in a directory, and I want to read only a few lines from each and then skip the rest of the text file and move on... load multiple files - comp.soft-sys.matlabReading a data file with the command 'load' - comp.soft-sys.math ... load multiple files - comp.soft-sys.matlab Reading a Line from a file - comp.soft-sys.matlab Well, the ... awk script to split text file content to multiple files - comp ...I want to read only a few lines from multiple text files - comp ... awk script to split text file content to multiple files - comp ..... blank line as record separator ... sed to insert lines in a file. - comp.unix.programmer1- read the manual : man sed a \ text Append text ... insert lines in a file. - comp.unix.programmer I need to insert multiple lines into a postscript file. handling multiple lines of input from inputdlg - comp.soft-sys ...Shortest way to read all lines (one by one) from a text file ..... because reading in multiple lines as ... way to read all lines (one by one) from a text file ... reading the previous line in a text file - comp.soft-sys.matlab ...Suppose I have multiple text files in a directory, and I want to read only a few lines from each and then skip the rest of the text file and move on... concatenating multiple text files into one text file - comp.soft ...Suppose I have multiple text files in a directory, and I want to ... I need to read txt files into MSAccess and ... Or, if the text file is just one continuous line - then ... Reading in BibTeX files using SAS - comp.soft-sys.sasI stopped because reading in multiple lines as one variable is a bit ... Sample SAS code: /* Read in BibTeX file as series of lines */ data rawtemp; length line $ ... read and merge lots of txt files, macro? - comp.soft-sys.sas ...load multiple files - comp.soft-sys.matlab read and merge lots of txt files, macro? - comp.soft-sys.sas ... Reading TXT file into MSAccess using Line ... how to comment out multiple lines in kshell? - comp.unix ...dlmwrite with no blank line at the end - comp.soft-sys.matlab ..... am reading my file into errors out when ... how to merge multiple lines into one line - comp ... reading text files - comp.soft-sys.sasSuppose I have multiple text files in a directory, and I want to read only a few lines from each and then skip the rest of the text file and move on... reading multiple lines from a txt file... - C and C++ - Forums at ...reading multiple lines from a txt file...: Hi i'm trying to read in all the lines of a txt file (that are in a specific format) and then store them to a vector of ... Reading multiple lines?Ok, this couldn't get much simpler - all I want to do is read multiple lines (or records) from a file. I could do this :- 7/23/2012 9:04:37 AM
|