I want to copy a binary master file in a new binary file. This file contain nothing but have a predefined size (20000 lines).
Here what i'm doing:
FILE *A_Lire;
FILE *A_Creer;
A_Lire = fopen(MASTERPath,"rb");
A_Creer = fopen(PARTPRGPath, "wb");
fseek(A_Lire,0,SEEK_END);
int end = ftell(A_Lire);
char* buf = (char*)malloc(end);
fread(buf,sizeof(char),end,A_Lire);
fwrite(buf,sizeof(char),end,A_Creer);
fclose(A_Creer);
fclose(A_Lire);
This code create the new file with the good size but this is not exactly the same file because I'm not able to used this new file like the master. Something is different, maybe corrupted, maybe the way to write in the file ???
Do you have any idea ???
I think this is MFC code
Thanks,
when you do fseek(..SEEK_END), the position inside the opened file is at the end, whenever you call fread, you are getting 0 bytes as you're at the end.
Just do a rewind after that:
fseek(A_Lire,0,SEEK_END);
int end = ftell(A_Lire);
fseek(A_Lire,0,SEEK_SET);
Related
This function print the length of words with '*' called histogram.How can I save results into text file? I tried but the program does not save the results.(no errors)
void histogram(FILE *myinput)
{
FILE *ptr;
printf("\nsaving results...\n");
ptr=fopen("results1.txt","wt");
int j, n = 1, i = 0;
size_t ln;
char arr[100][10];
while(n > 0)
{
n = fscanf(myinput, "%s",arr[i]);
i++;
}
n = i;
for(i = 0; i < n - 1; i++)
{
ln=strlen(arr[i]);
fprintf(ptr,"%s \t",arr[i]);
for(j=0;j<ln;j++)
fprintf(ptr, "*");
fprintf(ptr, "\n");
}
fclose(myinput);
fclose(ptr);
}
I see two ways to take care of this issue:
Open a file in the program and write to it.
If running with command line, change the output location for standard out
$> ./histogram > outfile.txt
Using the '>' will change where standard out will write to. The issue with '>' is that it will truncate a file and then write to the file. This means that if there was any data in that file before, it is gone. Only the new data written by the program will be there.
If you need to keep the data in the file, you can change the standard out to append the file with '>>' as in the following example:
$> ./histogram >> outfile.txt
Also, there does not have to be a space between '>' and the file name. I just do that for preference. It could look like this:
$> ./histogram >outfile.txt
If your writing to a file will be a one time thing, changing standard out is probably be best way to go. If you are going to do it every time, then add it to the code.
You will need to open another FILE. You can do this in the function or pass it in like you did the file being read from.
Use 'fprintf' to write to the file:
int fprintf(FILE *restrict stream, const char *restrict format, ...);
Your program may have these lines added to write to a file:
FILE *myoutput = fopen("output.txt", "w"); // or "a" if you want to append
fprintf(myoutput, "%s \t",arr[i]);
Answer Complete
There may be some other issues as well that I will discuss now.
Your histogram function does not have a return identifier. C will set it to 'int' automatically and then say that you do not have a return value for the function. From what you have provided, I would add the 'void' before the function name.
void histogram {
The size of arr's second set of arrays may be to small. One can assume that the file you are reading from does not exceed 10 characters per token, to include the null terminator [\0] at the end of the string. This would mean that there could be at most 9 characters in a string. Else you are going to overflow the location and potentially mess your data up.
Edit
The above was written before a change to the provided code that now includes a second file and fprintf statements.
I will point to the line that opens the out file:
ptr=fopen("results1.txt","wt");
I am wondering if you mean to put "w+" where the second character is a plus symbol. According to the man page there are six possibilities:
The argument mode points to a string beginning with one of the
following sequences (possibly followed by additional characters, as
described below):
r Open text file for reading. The stream is positioned at the
beginning of the file.
r+ Open for reading and writing. The stream is positioned at the
beginning of the file.
w Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.
w+ Open for reading and writing. The file is created if it does
not exist, otherwise it is truncated. The stream is
positioned at the beginning of the file.
a Open for appending (writing at end of file). The file is
created if it does not exist. The stream is positioned at the
end of the file.
a+ Open for reading and appending (writing at end of file). The
file is created if it does not exist. The initial file
position for reading is at the beginning of the file, but
output is always appended to the end of the file.
As such, it appears you are attempting to open the file for reading and writing.
I have an IDL routine that reads a binary data file. However, on this occasion, i'm getting "READU: End of file encountered. Unit 2, File: data.dat".
Instead of destroying the binary file and re-creating it. Is this problem surmountable? What IDL code could I use to allow me to read the binary file? The binary file was created by a C function.
Thanks in advance.
Based on the question, I'm assuming the binary file has a defined structure. You can probably use fstat() and eof() to get around this. For example:
openr, lun, 'file.bin', /get_lun
fs = fstat(lun)
len = fs.size / n_bytes_in_data_structure
for i = 0L, len - 1 do begin
readu, lun, var
...
If you don't know the size of your data structures or if you want to check that there's a sufficient number of bytes before a read, you can use fs.cur_ptr (after a call to fstat(), of course) or eof(lun).
I'm trying to add numbering to several lines of text in an existing .txt file using fopen's "r+" mode. This doesnt' seem to work and it ends up writing the first iteration of the string "line" followed by a large amount of junk value. Is there any way to add text at the beginning of a line? If so, am i coming at this the wrong way?
Also im trying to do this without having to write up a whole new file.
void main()
{
char read = ' ';
char buffer[25];
char line[4] = "01."; //lines from 01 to 99
FILE *file;
file = fopen("readme.txt","r+");
if (file == NULL)
{
printf("ERROR: Cannot open input file.\n");
exit();
}
do
{
fwrite(line,strlen(line),1,file);
read=gets(buffer);
if(!feof(file)) // updating line numbers
{
if(line[1]<'9')
{
(line[1])++;
}
else
{
if(line[0]<'9')
{
(line[0])++;
}
else
{
exit();
}
}
}
else
{
exit();
}
}while(!(feof(file)));
fclose(file);
exit();
}
Files in C let you overwrite and append, but not "prepend" data. To insert at the beginning or in the middle, you must copy the "tail" manually.
If you are writing a line-numbering program, it would be much simpler (and faster) to write the result into a separate temporary file, and then copy it in place of the original once the operation is complete.
You can use a simple loop that reads the original file line-by-line, and writes the output file, for example, with fprintf:
fprintf(outFile, "%02d.%s", lineNumber++, lineFromOrigFile);
No, there is no portable/standard way of doing what you want.
Files are random access, but you can't insert data into a file since that would force all the other data to move, which is not an operation supported by typical file systems.
The best solution is to do it in two steps:
Read through the input, while writing output to a new file
Rename the new file to replace the original input
While the answers are correct and you can't add a string in the beginning of a file in C (using only FILE commands), you can interact with the operating system and use bash command 'sed' (in Linux) that solve the problem pretty easily without creating a new file and copy the content of the older file.
void AddTextToFirstLine(const char* file_name, const char* text)
{
char command_string[100]; //Preferably some constant but can be done with malloc of size strlen(text) + strlen(file_name) + 16 (chars in the command + nullbyte)
sprintf(command_string, "sed -i '1 i\\%s' %s", text, file_name);
system(command_string); //executing the sed command
}
You can also look for the equivalent of 'sed' in Unix system for Windows: Is there any sed like utility for cmd.exe?
Although calling the operating system is not recommended usually (because every operating system action stops the program flow and therefore damage time efficiency) This is one of the rare cases in which its ok since we're calling the operating system anyway when we're creating a file (or a copy file in that case). Therefore using this method will also decrease the running time compare to the solution of creating a copy file and copy the content of the old file to it.
Adding string in the beginning of file is just like inserting.
And you can't really directly insert string using C, instead you will overwrite old content.
So you can only do an overhaul to the text file:
record old file content somewhere(temp file or memory...etc), write your string, then paste old content back.
I have opened one file with following way:
fp = fopen("some.txt","r");
Now in this file the 1st some bytes lets say 40 bytes are unnecessary junk of data so I want to remove them. But I cannot delete that data from that file, modify or
create duplicates of that file without that unnecessary data.
So I want to create another dummy FILE pointer which points to the file and when I pass this dummy pointer to any another function that does the following operation:
fseek ( dummy file pointer , 0 , SEEK_SET );
then it should set the file pointer at 40th position in my some.txt.
But the function accepts a file descriptor so i need to pass a file descriptor which will treat the file as those first 40 bytes were never in the file.
In short that dummy descriptor should treat the file as those 40 bytes were not in that file and all positioning operations should be with respect to that 40th byte counting as the is 1st byte.
Easy.
#define CHAR_8_BIT (0)
#define CHAR_16_BIT (1)
#define BIT_WIDTH (CHAR_8_BIT)
#define OFFSET (40)
FILE* fp = fopen("some.txt","r");
FILE* dummy = NULL;
#if (BIT_WIDTH == CHAR_8_BIT)
dummy = fseek (fp, OFFSET*sizeof(char), SEEK_SET);
#else
dummy = fseek (fp, OFFSET*sizeof(wchar_t), SEEK_SET);
#endif
The SEEK_SET macro indicates beginning of file, and depending on whether you are using 8-bit characters (ASCI) or 16-bit characters (eg: UNICODE) you will step 40 CHARACTERS forward from the beginning of your file pointer, and assign that pointer/address to dummy.
Good luck!
These links will likely be helpful as well:
char vs wchar_t
http://www.cplusplus.com/reference/clibrary/cstdio/fseek/
If you want, you can just convert a file descriptor to a file pointer via the fdopen() call.
http://linux.die.net/man/3/fdopen
fseek ( dummy file pointer , 0 , SEEK_SET );
In short that dummy pointer should treat the file as there is no that 40 byte in that file and all position should be with respect to that 40th byte as counting as it is 1st byte.
You have conflicting requirements, you cannot do this with the C API.
SEEK_SET always refers to the absolute position in the file, which means if you want that command to work, you have to modify the file and remove the junk.
On linux you could write a FUSE driver that would present the file like it was starting from the 40th byte, but that's a lot of work. I'm only mentioned this because it's possible to solve the problem you've created, but it would be quite silly to actually do this.
The simplest thing of course would be just to abandon this emulating layer idea you're looking for, and write code that can handle that extra header junk.
If you want to remove the first 40 bytes of a file on the disk without creating another file, then you can copy the content from the 41th byte and onwards into a buffer, then write it back at offset -40. Then use ftruncate (a POSIX library in unistd.h) to truncate at (filesize - 40) offset.
I wrote a small code with what i understood from your question.
#include<stdio.h>
void readIt(FILE *afp)
{
char mystr[100];
while ( fgets (mystr , 100 , afp) != NULL )
puts (mystr);
}
int main()
{
FILE * dfp = NULL;
FILE * fp = fopen("h4.sql","r");
if(fp != NULL)
{
fseek(fp,10,SEEK_SET);
dfp = fp;
readIt(dfp);
fclose(fp);
}
}
The readIt() is reading the file from the 11 byte.
Is this what you are expecting or something else?
I haven't actually tried this, but I think you should be able to use mmap (with the MAP_SHARED option) to get your file mapped into your address space, and then fmemopen to get a FILE* that refers to all but the first 40 bytes of that buffer.
This gives you a FILE* (as you describe in the body of your question), but I believe not a file descriptor (as in the title and elsewhere in the question). The two are not the same, and AFAIK the FILE* created with fmemopen does not have an associated file descriptor.
I have been given a raw file that holds several jpg images. I have to go through the file, find each jpg image, and put those images each in a separate file. So far I have code that can find each where each image begins and ends. I also have written code that names several file names I can use to put the pictures in. It is an array: char filename[] , that holds the names: image00.jpg - image29.jpg .
What I cannot figure out is how to open a file every time I find an image, an then close that file and open a new one for the next image. Do I need to use fwrite()? Also, each image is in blocks of 512 bytes, so I only have to check for a new image every 512 bytes once I find the first one. Do I need to add that into fwrite?
So, to summarize my questions, I don't understand how to use fwrite(), if that is what I should be using to write to these files.
Also, I do not know how to open the files using the names I have already created.
Thanks in advance for the help. Let me know if I need to post any other code.
Use fopen(rawfilename, "rb"); to open the raw file for reading. and fread to read from it.
Use fopen(outfilename, "wb"); to open output file for writing and fwrite to write to it.
As mentioned in my comment, you are assigning char *[] to char*, use char filename[] = "image00.jpg"; instead.
Don't forget to close each file after you finish its processing (r/w) (look at fclose() at the same site of other links)
Decide how much bytes to read each time by parsing the jpeg header. Use malloc to allocate the amount of bytes needed to be read, and remember, for each allocation of buffer you need to free the allocated buffer later.
Pretty much any book on C programming should cover the functions you need. As MByD pointed out, you'll want to use the functions fopen(), fwrite(), and fclose().
I imagine your code may include fragments that look something like
/* Warning: untested and probably out-of-order code */
...
char **filename = {
"image00.jpg", "image01.jpg", "image02.jpg",
...
"image29.jpg" };
...
int index = 0;
const int blocksize = 512; /* bytes */
...
index++;
...
FILE * output_file = fopen( filename[index], "wb");
fwrite( output_data, 1, blocksize, output_file );
fclose(output_file);
...