-
File troubles
I am working on a program to manipulate files. Data is read
from input file and is to be transposed into the output file.
Here is the description :
Input File
^A^@^U^G^L^G^Q^G^F^h
^H^P^a^j^i^k^l^m^f^x
and multiple lines like this. Specs. indicate each line can
be upto 32MB!!!.
I am printing the decimal equivalent of these characters in the
output file. Output has to be like :
/* I have copied input chars. to illustrate. In reality output file
has decimal equivalents of input chars. */
^A^H
^@^P
^U^a
^G^j
^L^i
^G^k
^Q^l
^G^m
^F^f
^h^x
and so on.
What I have done is to read each line into a 'unsigned char buffer'. Find the length of the buffer, print each character
of the buffer, add a newline and advance file pointer.
fp1 = Output file pointer
length = Length of input buffer(line)
buf = unsigned char buffer
/** Snippet of code **/
for(index = 0 ; index < length ; index++)
{
pos = ftell(fp1) ;
fprintf(fp1,"%d",buf[index]);
fprintf(fp1,"\n");
pos = ftell(fp1) ;
fseek(fp1,pos+(length/2)-1,SEEK_SET);
}
iteration+= 1 ; /* Moves the fp1 to beginnig of 2nd col. */
fseek(fp1,iteration,SEEK_SET);
/********************/
This does print the data as I want it. But it also adds extra
characters to the output file(I believe newline). I do not want
that. Is there is a way it can be accomplished without adding
'extra/new' characters in the output file.
PS : The input files can be very huge, as they are generated by
a automatic chip tester. Each line can be upto 32MB !!!.So far
I have worked on only simple file stuff.
Any inputs are really appreciated.
Thanks in advance.
-
fopen("myfile.xyz", "rb")
fopen("myfile.abc","wb")
Binary is your friend.
Quzah.
-
File Troubles
Salem,
The code snippet is the actual code I have used to print the
buffer contents. Buffer contains the data from the input file.
I am opening files and reading the input file contents using
the following statements.
/* Opening input and output file */
fp = fopen("input_file", "rb");
fp1 = fopen("output_file,"wb");
/* Reading from input file */
fread(buf,sizeof(char), MAX_SIZE,fp) ;
length = strlen(buf) ;
Quzah ,
I am using binary mode to open files.
Thanks.
-
> fread(buf,sizeof(char), MAX_SIZE,fp) ;
> length = strlen(buf) ;
So long as buf is
char buf[MAX_SIZE];
The killer is strlen - you can't use strlen on a binary string - it will stop at the first \0
Do this
length = fread(buf,sizeof(char), MAX_SIZE,fp) ;