read from file question

A

Arquitecto

Hi ,

I have a question about a file operation i want to do . I have a data
file lets say X bytes . I want to read the file and delete a byte
every 2nd byte . I am a little comfused ,my approach is like this .

main()
{
char buf[1024];
FILE *fp;
fp=fopen("data", "rb");

while(!feof(fp)) {
fread(buf, 1, 1, fp);
}

fclose(fp);
}

ok ,with this i can read 1 byte at a time till end .Now what i want to
do is .... i want to have on buf the 1 byte ,3rd byte .. 5th .. etc
and discard 2nd .. 4th ...etc .My thought was to take the position of
the file pointer ,
example : position= ftell(fp) and if position % 2 == 0 then do not
read ..

but i dont know if i am on right way or is a correct way to do what i
want .I will appreciate comments on how to approach it . thanks in
advance .
 
I

Ian Malone

Arquitecto said:
Hi ,

I have a question about a file operation i want to do . I have a data
file lets say X bytes . I want to read the file and delete a byte
every 2nd byte . I am a little comfused ,my approach is like this .

main()
{
char buf[1024];
FILE *fp;
fp=fopen("data", "rb");

while(!feof(fp)) {
fread(buf, 1, 1, fp);
}

fclose(fp);
}


This doesn't do what you think; you repeatedly read
a single byte into the first element of buf[].

1. Prefer to check the return value of fread instead
of testing feof. fread may encounter errors before
getting to end of file.

2. To use fread to read one byte at a time you'll need
a pointer to the current position in the buffer.
ok ,with this i can read 1 byte at a time till end .Now what i want to
do is .... i want to have on buf the 1 byte ,3rd byte .. 5th .. etc
and discard 2nd .. 4th ...etc .My thought was to take the position of
the file pointer ,
example : position= ftell(fp) and if position % 2 == 0 then do not
read ..

but i dont know if i am on right way or is a correct way to do what i
want .I will appreciate comments on how to approach it . thanks in
advance .

So you can try to read the whole file at once and then copy
every other char to your result buffer, or read the file one
byte at a time and put only the odd indexed bytes into your
buffer (remembering that you need to keep track of the current
buffer position). There are a few ways you can do that, but
you can do it without using ftell.
 
O

osmium

Arquitecto said:
I have a question about a file operation i want to do . I have a data
file lets say X bytes . I want to read the file and delete a byte
every 2nd byte . I am a little comfused ,my approach is like this .

main()
{
char buf[1024];
FILE *fp;
fp=fopen("data", "rb");

while(!feof(fp)) {
fread(buf, 1, 1, fp);
}

fclose(fp);
}

ok ,with this i can read 1 byte at a time till end .Now what i want to
do is .... i want to have on buf the 1 byte ,3rd byte .. 5th .. etc
and discard 2nd .. 4th ...etc .My thought was to take the position of
the file pointer ,
example : position= ftell(fp) and if position % 2 == 0 then do not
read ..

but i dont know if i am on right way or is a correct way to do what i
want .I will appreciate comments on how to approach it . thanks in
advance .

I would do it like this. Create an output buffer and put the desired data
in it using the indexing operations. Then write the output buffer to a new
file. When you are done, delete the original file and rename the new file
to have the name of the old file.
 
K

Keith Thompson

osmium said:
Arquitecto said:
I have a question about a file operation i want to do . I have a data
file lets say X bytes . I want to read the file and delete a byte
every 2nd byte .
[...]
I would do it like this. Create an output buffer and put the
desired data in it using the indexing operations. Then write the
output buffer to a new file. When you are done, delete the original
file and rename the new file to have the name of the old file.

That implies storing half the contents of the input file in memory,
without knowing how big the input file is. Allocating memory for this
can be tricky; realloc should do it *if* it's possible at all.

But it's not necessary. As you're reading the input file, write every
other character to the output file. When you're done, close the input
and output files and rename the output file. (And do error checking.)
 
C

CBFalconer

Arquitecto said:
I have a question about a file operation i want to do . I have a
data file lets say X bytes . I want to read the file and delete a
byte every 2nd byte . I am a little comfused ,my approach is like
this .

.... snip code ...

Try opening an input and output file (or use stdin and stdout) and:

int n, ch;

n = 1;
while (EOF != (ch = getchar())) {
n = -n;
if (n < 0) putchar(ch);
}

Then close the files. Note that ch is an int. This is only a
snippet. Look up fopen and fclose, also stdio.h. Test fopen for
success, also fclose if you are going to destroy the original file.
 
O

osmium

Keith Thompson said:
osmium said:
Arquitecto said:
I have a question about a file operation i want to do . I have a data
file lets say X bytes . I want to read the file and delete a byte
every 2nd byte .
[...]
I would do it like this. Create an output buffer and put the
desired data in it using the indexing operations. Then write the
output buffer to a new file. When you are done, delete the original
file and rename the new file to have the name of the old file.

That implies storing half the contents of the input file in memory,
without knowing how big the input file is. Allocating memory for this
can be tricky; realloc should do it *if* it's possible at all.

No, it implies a 1024K input buffer and a 512K output buffer, based on my
quick reading of what the OP was up to.

I'll do the implying and you do the inferring, that's the way it's usually
done.
 
A

Army1987

Arquitecto said:
Hi ,

I have a question about a file operation i want to do . I have a data
file lets say X bytes . I want to read the file and delete a byte
every 2nd byte . I am a little comfused ,my approach is like this .

main()
{
char buf[1024];
FILE *fp;
fp=fopen("data", "rb");

while(!feof(fp)) {
fread(buf, 1, 1, fp);
}

fclose(fp);
}

ok ,with this i can read 1 byte at a time till end .Now what i want to
do is .... i want to have on buf the 1 byte ,3rd byte .. 5th .. etc
and discard 2nd .. 4th ...etc .My thought was to take the position of
the file pointer ,
example : position= ftell(fp) and if position % 2 == 0 then do not
read ..

but i dont know if i am on right way or is a correct way to do what i
want .I will appreciate comments on how to approach it . thanks in
advance .

Try this: (not compiled, not tested)
#include <stdio.h>
#include <stdlib.h> /* for EXIT_FAILURE */
int main(void)
{
char buf[1024];
int ch;
int i;
FILE *fp = fopen("data", "rb");
if (fp == NULL) {
perror("Read error");
return EXIT_FAILURE;
}
for (i = 0; i < sizeof buf; i++) {
ch = getc(fp);
if (ch == EOF)
break;
else
buf = ch;
(void)getc(fp); /*discard odd bytes*/
}
fclose(fp);
return 0;
}
 
A

Arquitecto

Nice Army, worked :) i also allocated buf memory dynamically with
calloc and now works nice xD ,thanks everyone :)


Army1987 :
Arquitecto said:
Hi ,

I have a question about a file operation i want to do . I have a data
file lets say X bytes . I want to read the file and delete a byte
every 2nd byte . I am a little comfused ,my approach is like this .

main()
{
char buf[1024];
FILE *fp;
fp=fopen("data", "rb");

while(!feof(fp)) {
fread(buf, 1, 1, fp);
}

fclose(fp);
}

ok ,with this i can read 1 byte at a time till end .Now what i want to
do is .... i want to have on buf the 1 byte ,3rd byte .. 5th .. etc
and discard 2nd .. 4th ...etc .My thought was to take the position of
the file pointer ,
example : position= ftell(fp) and if position % 2 == 0 then do not
read ..

but i dont know if i am on right way or is a correct way to do what i
want .I will appreciate comments on how to approach it . thanks in
advance .

Try this: (not compiled, not tested)
#include <stdio.h>
#include <stdlib.h> /* for EXIT_FAILURE */
int main(void)
{
char buf[1024];
int ch;
int i;
FILE *fp = fopen("data", "rb");
if (fp == NULL) {
perror("Read error");
return EXIT_FAILURE;
}
for (i = 0; i < sizeof buf; i++) {
ch = getc(fp);
if (ch == EOF)
break;
else
buf = ch;
(void)getc(fp); /*discard odd bytes*/
}
fclose(fp);
return 0;
}
 
K

Keith Thompson

osmium said:
Keith Thompson said:
osmium said:
:
I have a question about a file operation i want to do . I have a data
file lets say X bytes . I want to read the file and delete a byte
every 2nd byte . [...]
I would do it like this. Create an output buffer and put the
desired data in it using the indexing operations. Then write the
output buffer to a new file. When you are done, delete the original
file and rename the new file to have the name of the old file.

That implies storing half the contents of the input file in memory,
without knowing how big the input file is. Allocating memory for this
can be tricky; realloc should do it *if* it's possible at all.

No, it implies a 1024K input buffer and a 512K output buffer, based on my
quick reading of what the OP was up to.

I'll accept that that's what you meant, but it's not what you wrote.
You didn't mention a specific buffer size (isn't a 1-megabyte input
buffer bigger than it needs to be?), nor did you mention or imply a
loop.

If I were doing this, I'd just use fgetc() and fputc(), writing every
other input character to the output file, and let the stdio
implementation take care of any buffering. That also has the
advantage of not needing special-case code if the file size isn't a
multiple of the input buffer size (though that's not a huge deal).
I'll do the implying and you do the inferring, that's the way it's usually
done.

And that's exactly what I did.
 
C

CBFalconer

Arquitecto wrote: *** and top-posted - fixed ***
Army1987 said:
Arquitecto said:
I have a question about a file operation i want to do . I have a
data file lets say X bytes . I want to read the file and delete
a byte every 2nd byte . I am a little comfused ,my approach is
like this .
.... snip ...

Try this: (not compiled, not tested)
#include <stdio.h>
#include <stdlib.h> /* for EXIT_FAILURE */
int main(void)
{
char buf[1024];
int ch;
int i;
FILE *fp = fopen("data", "rb");
if (fp == NULL) {
perror("Read error");
return EXIT_FAILURE;
}
for (i = 0; i < sizeof buf; i++) {
ch = getc(fp);
if (ch == EOF)
break;
else
buf = ch;
(void)getc(fp); /*discard odd bytes*/
}
fclose(fp);
return 0;
}


Nice Army, worked :) i also allocated buf memory dynamically with
calloc and now works nice xD ,thanks everyone :)


Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. I fixed this one. See the following links:

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 
S

SM Ryan

# Hi ,
#
# I have a question about a file operation i want to do . I have a data
# file lets say X bytes . I want to read the file and delete a byte
# every 2nd byte . I am a little comfused ,my approach is like this .

With careful use of fseek and fread, you can modify the file in
place, but it simpler to scan the file once, writing every other
byte to a temporary file or saved to an internal buffer (if you have
enough memory), and the copy back to the original file.

Also you may need some way to truncate the file to half its size.
fopen(...,"r")...fclose(...)...fopen(...,"w") is one way to
truncate the file. Your system may have additional calls like
ftruncate.
 

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


Members online

Forum statistics

Threads
474,266
Messages
2,571,076
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top