|
-
March 16th, 2005, 12:29 AM
#1
Removing "N" bytes from file..???
I have the following question :
how to remove "N" number of bytes from a
opened text or binary file?
any idea?
-
March 16th, 2005, 12:46 AM
#2
Re: Removing "N" bytes from file..???
Code:
FILE* hFile;
hFile = fopen("c:\\numbers.txt", "r");
if(hFile)
{
char szText;
while(fread(&szText, sizeof(char), 1, hFile) > 0)
{
printf("%c",szText);
}
fclose(hFile);
}
You can open a file and read character by character. Open another file and write the desired data to the new file.
You also can use fstream for better control.
Last edited by jawadhashmi; March 16th, 2005 at 12:55 AM.
-
March 16th, 2005, 03:54 AM
#3
Re: Removing "N" bytes from file..???
Well, you want to remove N bytes from a file, starting at byte M.
* read the first M bytes from the file and write them in a temporary file
* read the next N bytes and skip them (do not copy them to the temp file)
* read the rest of the file and write them to the temp file
* delete the original file
* change the name of the temp file to the name of the orginal file (assuming they are in the same folder)
-
March 16th, 2005, 04:05 AM
#4
Re: Removing "N" bytes from file..???
well . actually i had put one Q under the thread called "File Splitting".
There were no replies for that.
My purpose is :
1.Read first 200 bytes from a file (File A ) into a buffer.
2. Remove first 4 bytes from that buffer and write to a new file (File B).
3. Read next 200 bytes from the orginal file (File A) .
4. Perform step 2 .
5. Perform this until the end of the orgimal file (File A).
Did u get an idea what i wanted to do?
-
March 16th, 2005, 04:28 AM
#5
Re: Removing "N" bytes from file..???
You can see the code above to read the data from the file. You can read 200 bytes at a time from the file.
open the another file and start writting to it from 5th byte.
repeat the reading and writting operation untill there is no more data in the file.
are you looking for the complete program?
-
March 16th, 2005, 04:34 AM
#6
Re: Removing "N" bytes from file..???
i am in the learning stage of C/C++.
If u can give complete program with comment and explanation
it will be greatly helpful to learn C file operations.
thanks.
-
March 16th, 2005, 05:17 AM
#7
Re: Removing "N" bytes from file..???
Code:
FILE* f_in;
FILE* f_out;
//open file for reading
f_out = fopen("C:\\file1.txt", "r");
//open file for writting
f_in = fopen("c:\\file2.txt", "a+");
char szData[200] = "";
//read the file to the end
int read = 0;
while( (read = fread(szData, sizeof(char), 200, f_out)) > 0)
{
if(read > 4)
{
fwrite(&szData[4], sizeof(char), strlen(szData)-4, f_in);
}
}
//close the reading file
fclose(f_out);
//close the writting file
fclose(f_in);
Hopefully this will help you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|