why doesn't this code work? :)

B

buda

All error checking is removed for clarity. This turns into a infinite loop
that writes at the end of a file, and makes it grow until a "forced brake".
Thanks for the help :) (btw, both stdio.h and stdlib.h are included)

int zamijeni (char *ime_datoteke) {
FILE *fp;
int t, flag = 0;

fp = fopen( ime_datoteke, "r+b" );
while ( fread( (void *)&t, (long)sizeof( t ), 1, fp ) == 1 ) {
if ( t < 0 ) {
t = -t;
++flag;
fseek( fp, -1L * (long)sizeof( t ), SEEK_CUR );
fwrite( (void *)&t, (long)sizeof( t ), 1, fp );
}
}
return flag > 0;
}
 
L

Luis Diego

char *lineptr[MAXLINE];

With this declaration you have a pointer to an array and each field of the
array is an "char *"

The problem is that each field in lineptr array must be allocated (because
they are pointers).
So you need to change your code:
char *lineptr[MAXLINE];
...
...
...

for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++) {
printf("%s",line);
/* here i would like to do something */
/* like */
/* strcpy(lineptr, line); */
/* or */
/* lineptr = line; */
}



char lineptr[MAXLINE][LINESIZE];

for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++)
{
printf("%s",line);
strcpy(lineptr, line);

/*The following commente line can't be used, would be a big mistake*/
/* lineptr = line; */
}

I don't promise it would works AS IS.

I strongly recommend "The C Programming Language" book from BW Kernighan and
DM Ritchie, great book to start with C.


Ing. Luis Diego Bolanos Quiros
NPY & PE Dept.
(e-mail address removed)
Inet 8-256-6536
Office(506) 298-8939
Cell (506) 817-6566
 
L

Luis Diego

sorry I make a mistake, this is for another Post

: D

Luis Diego said:
char *lineptr[MAXLINE];

With this declaration you have a pointer to an array and each field of the
array is an "char *"

The problem is that each field in lineptr array must be allocated (because
they are pointers).
So you need to change your code:
char *lineptr[MAXLINE];
...
...
...

for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++) {
printf("%s",line);
/* here i would like to do something */
/* like */
/* strcpy(lineptr, line); */
/* or */
/* lineptr = line; */
}



char lineptr[MAXLINE][LINESIZE];

for (i=0; fgets(line, LINESIZE, fp) != NULL ; i++)
{
printf("%s",line);
strcpy(lineptr, line);

/*The following commente line can't be used, would be a big mistake*/
/* lineptr = line; */
}

I don't promise it would works AS IS.

I strongly recommend "The C Programming Language" book from BW Kernighan and
DM Ritchie, great book to start with C.


Ing. Luis Diego Bolanos Quiros
NPY & PE Dept.
(e-mail address removed)
Inet 8-256-6536
Office(506) 298-8939
Cell (506) 817-6566

buda said:
All error checking is removed for clarity. This turns into a infinite loop
that writes at the end of a file, and makes it grow until a "forced brake".
Thanks for the help :) (btw, both stdio.h and stdlib.h are included)

int zamijeni (char *ime_datoteke) {
FILE *fp;
int t, flag = 0;

fp = fopen( ime_datoteke, "r+b" );
while ( fread( (void *)&t, (long)sizeof( t ), 1, fp ) == 1 ) {
if ( t < 0 ) {
t = -t;
++flag;
fseek( fp, -1L * (long)sizeof( t ), SEEK_CUR );
fwrite( (void *)&t, (long)sizeof( t ), 1, fp );
}
}
return flag > 0;
}
 
F

Francois Grieu

"buda" <[email protected]> said:
All error checking is removed for clarity. This turns into a infinite loop
that writes at the end of a file, and makes it grow until a "forced brake".
Thanks for the help :) (btw, both stdio.h and stdlib.h are included)

int zamijeni (char *ime_datoteke) {
FILE *fp;
int t, flag = 0;

fp = fopen( ime_datoteke, "r+b" );
while ( fread( (void *)&t, (long)sizeof( t ), 1, fp ) == 1 ) {
if ( t < 0 ) {
t = -t;
++flag;
fseek( fp, -1L * (long)sizeof( t ), SEEK_CUR );
fwrite( (void *)&t, (long)sizeof( t ), 1, fp );
}
}
return flag > 0;
}

Could the fwrite fail ? e.g. file is locked, on read-only media..

Also: with a C compiler, you can remove (void *) and (long);
and there is a risk that flag overflows to 0.


/* return -1 if error, 1 if negatives found and fixed, else 0 */
int zamijeni (char *ime_datoteke) {
FILE *fp;
int t, flag = 0;

if ((fp = fopen( ime_datoteke, "r+b"))!=NULL)
return -1;
while ( fread(&t, sizeof t, 1, fp )==1) {
if ( t < 0 ) {
t = -t;
flag = 1;
if (fseek( fp, -sizeof t, SEEK_CUR )!=0
|| fwrite(&t, sizeof t, 1, fp )!=1)
return -1;
}
if (fclose(fp)!=0)
return -1;
}
return flag;
}


François Grieu
 
F

Francois Grieu

"buda" <[email protected]> said:
All error checking is removed for clarity. This turns into a infinite loop
that writes at the end of a file, and makes it grow until a "forced brake".
Thanks for the help :) (btw, both stdio.h and stdlib.h are included)

int zamijeni (char *ime_datoteke) {
FILE *fp;
int t, flag = 0;

fp = fopen( ime_datoteke, "r+b" );
while ( fread( (void *)&t, (long)sizeof( t ), 1, fp ) == 1 ) {
if ( t < 0 ) {
t = -t;
++flag;
fseek( fp, -1L * (long)sizeof( t ), SEEK_CUR );
fwrite( (void *)&t, (long)sizeof( t ), 1, fp );
}
}
return flag > 0;
}

Could the fwrite fail ? e.g. file is locked, on read-only media..

Also: with a C compiler, you can remove (void *) and (long);
and there is a risk that flag overflows to 0.


/* return -1 if error, 1 if negatives found and fixed, else 0 */
int zamijeni (char *ime_datoteke) {
FILE *fp;
int t, flag = 0;

if ((fp = fopen( ime_datoteke, "r+b"))==NULL)
return -1;
while ( fread(&t, sizeof t, 1, fp )==1) {
if ( t < 0 ) {
t = -t;
flag = 1;
if (fseek( fp, -sizeof t, SEEK_CUR )!=0
|| fwrite(&t, sizeof t, 1, fp )!=1)
return -1;
}
if (fclose(fp)!=0)
return -1;
}
return flag;
}


François Grieu
 
B

buda

Could the fwrite fail ? e.g. file is locked, on read-only media..

Also: with a C compiler, you can remove (void *) and (long);
and there is a risk that flag overflows to 0.


/* return -1 if error, 1 if negatives found and fixed, else 0 */
int zamijeni (char *ime_datoteke) {
FILE *fp;
int t, flag = 0;

if ((fp = fopen( ime_datoteke, "r+b"))==NULL)
return -1;
while ( fread(&t, sizeof t, 1, fp )==1) {
if ( t < 0 ) {
t = -t;
flag = 1;
if (fseek( fp, -sizeof t, SEEK_CUR )!=0
|| fwrite(&t, sizeof t, 1, fp )!=1)
return -1;
}
if (fclose(fp)!=0)
return -1;
}
return flag;
}


François Grieu

No, none of the file functions ever fails (the running code has a bunch of
error checking, but it has no effect; also, i ran it trough the debugger and
saw for myself that the fread and fwrite both return 1 on each pass, and
fseek returns 0 which is also OK).

Btw, I know about the casts, but I guess it's a habbit from back when I
didn't know you didn't need them :) (or better yet, when I used them as a
matter of style in this sort of situations :)
 
M

Martin Dickopp

buda said:
All error checking is removed for clarity. This turns into a infinite loop
that writes at the end of a file, and makes it grow until a "forced brake".
Thanks for the help :) (btw, both stdio.h and stdlib.h are included)

int zamijeni (char *ime_datoteke) {
FILE *fp;
int t, flag = 0;

fp = fopen( ime_datoteke, "r+b" );
while ( fread( (void *)&t, (long)sizeof( t ), 1, fp ) == 1 ) {
if ( t < 0 ) {
t = -t;
++flag;
fseek( fp, -1L * (long)sizeof( t ), SEEK_CUR );
fwrite( (void *)&t, (long)sizeof( t ), 1, fp );
}
}
return flag > 0;
}

A write operation to a stream may not be directly followed by a read
operation from the same stream. Between them, the stream must be
flushed or the file position must be changed. (The file position must
also be changed if writing follows reading, and that's what you do with
the `fseek' call.)

Insert `fflush(fp);' after the `fwrite' call.

Martin
 
B

buda

Thank you very much.

Martin Dickopp said:
A write operation to a stream may not be directly followed by a read
operation from the same stream. Between them, the stream must be
flushed or the file position must be changed. (The file position must
also be changed if writing follows reading, and that's what you do with
the `fseek' call.)

Insert `fflush(fp);' after the `fwrite' call.

Martin


--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
 
B

Barry Schwarz

Could the fwrite fail ? e.g. file is locked, on read-only media..

Also: with a C compiler, you can remove (void *) and (long);
and there is a risk that flag overflows to 0.

Since flag is a signed int, any overflow leads to undefined behavior.
/* return -1 if error, 1 if negatives found and fixed, else 0 */
int zamijeni (char *ime_datoteke) {
FILE *fp;
int t, flag = 0;

if ((fp = fopen( ime_datoteke, "r+b"))==NULL)
return -1;
while ( fread(&t, sizeof t, 1, fp )==1) {
if ( t < 0 ) {
t = -t;
flag = 1;
if (fseek( fp, -sizeof t, SEEK_CUR )!=0
|| fwrite(&t, sizeof t, 1, fp )!=1)
return -1;
}
if (fclose(fp)!=0)
return -1;
}
return flag;
}


François Grieu



<<Remove the del for email>>
 

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

Similar Threads

Help with EXT3 Filesystem work 1
URGENT 1
Qsort() messing with my entire Code 0
Binary File I/O 11
Why this doesen't work 11
Why this doesen't work...? 3
Why this doesn't work 19
code 34

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top