Following is my program. I try to read the file rgb_values.txt and transfer its data to out.txt. rgb_values.txt is very long with about 2.7 million lines, but when I run the program and check out.txt; it has only 2.5 million lines (2554994 lines to be exact) which means that in this line the program reads to EOF. However, this should not be the case. I don't know what went wrong
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include <stdio.h>
int count=0;
void getOneByte(FILE *plaintext,unsigned char *pt,int i){
char line[4]={0x0};
fgets(line,sizeof(line),plaintext);
int x1=0x0;
char str[4];
sprintf(str,"%s",line);
sscanf(str,"%02x ",&x1);
pt[i]=x1;
}
void get16Bytes(FILE *plaintext,unsigned char * pt){
int i=0;
char sh;
for (int i = 0; i <=15; i++)
{
getOneByte(plaintext,pt,i);
if((sh=fgetc(plaintext))!=EOF){
ungetc(sh,plaintext);
}
else{
ungetc(sh,plaintext);
break;
}
}
}
void write16Bytes(FILE *ciphertext,unsigned char *ct){
int i;
for(i = 0; i < 16; i++){
if(count == 0){
fprintf(ciphertext,"%02x",ct[i]);
count++;
}
else if(count == 1){
fprintf(ciphertext," %02x",ct[i]);
count++;
}
else if(count == 2){
fprintf(ciphertext," %02x\n",ct[i]);
count = 0;
}
}
}
int main(){
FILE *fp=fopen("rgb_values.txt","r");
FILE *fo=fopen("out.txt","w");
char ch;
unsigned char pt[16];
int i=15;
int cnt=0;
while(1){
memset(pt,'a',sizeof(pt));
get16Bytes(fp,pt);
if((ch=fgetc(fp))!=EOF){
ungetc(ch,fp);
}
else{
break;
}
write16Bytes(fo,pt);
cnt++;
}
fclose(fp);
return 1;
}
Link for rgb_values.txt
Link for my out.txt
Part of my input file:
c1 c1 c1
ff ff ff
ff ff ff
ff ff ff
ff ff ff
fe fe fe
fd fd fd
ff ff ff
fb fb fb
fe fe fe
fe fe fe
ff ff ff
fb fb fb
f1 f1 f1
e9 e9 e9
e6 e6 e6
e5 e5 e5
e5 e5 e5
e5 e5 e5
e5 e5 e5
e5 e5 e5
e5 e5 e5
e5 e5 e5
e5 e5 e5
e5 e5 e5
e5 e5 e5
e5 e5 e5
e2 e2 e2
e5 e5 e5
e7 e7 e7
e5 e5 e5
e4 e4 e4
e3 e3 e3
e3 e3 e3
e8 e8 e8
de de de
9c 9c 9c
3b 3b 3b
01 01 01
02 02 02
00 00 00
02 02 02
0a 0a 0a
I don't know what went wrong
Coding is not defensive as it does not look for unexpected events.
To learn what went wrong, improve code's error detection.
In other words, don't trust user input - its evil.
Why read only 3 bytes?
The below reads, at most 3 bytes from the file, leaving the remainder of a line for later reading.
char line[4]={0x0};
fgets(line,sizeof(line),plaintext);
It does not read only 3 bytes from a file and toss the remainder of the line.
Check return value of fgets()
// fgets(line,sizeof(line),plaintext);
if (fgets(line,sizeof(line),plaintext) == NULL) {
TBD_alert();
}
Use int
int fgetc() can return 257 different values. Saving in a char loses something.
// char ch;
int ch;
// char sh;
int sh;
Lack of error checking
What if sscanf(str,"%02x ",&x1) returns 0 (no conversion)?
What if 4 is too small?
Should an extra white-space exist, (space, '\r', ...), 4 is simply too small.
char line[4]={0x0};
fgets(line,sizeof(line),plaintext);
Instead read a complete line with fgets() allowing at least a 2x size buffer over expected size and tolerate extra white-space or a missing '\n' on the last line. Given a line of "c1 c1 c1\n", use a buffer size of (3*3 + 1)*2.
I suspect many '\r' lurking about.
Avoid UB. Use matching specifiers
"%x" matches an unsigned, not int.
// int x1=0x0;
unsigned x1 = 0x0;
...
// '0' is useless, '2' is not needed. Trailing space is useless
// sscanf(str,"%02x ",&x1);
sscanf(str,"%x", &x1); // Check return value - not shown
or saved directly
sscanf(str, "%hhx", &pt[i]);
What is other than 3 values per line?
Sample code to read a line of 3 hex values and detect lots of errors.
#define EXPECTED_LINE_SIZE (3*3 + 1 /* for the \0 */)
char line[EXPECTED_LINE_SIZE * 2];
if (fgets(line, sizeof line, plaintext) == NULL) {
return failure;
}
int n = 0;
unsigned val[3];
sscanf(line "%2x %2x %2x %n", &val[0], &val[1], &val[2], &n);
if (n == 0 || line[n] != '\0') {
return failure;
}
Check if fopen() failed
FILE *fp=fopen("rgb_values.txt","r");
FILE *fo=fopen("out.txt","w");
if (fp == NULL || fo == NULL) {
TBD_Error_out(); // Add your code here
}
If you just want to copy from one file to the other, open in binary mode, and in a loop fread a buffer of say 4KiB, and then fwrite it to the other file. Quick and easy:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *in = fopen(..., "rb");
FILE *out = fopen(..., "wb");
if (!in || !out)
return EXIT_FAILURE;
char buffer[4096];
size_t nread;
while ((nread = fread(buffer, 1, sizeof buffer, in)) > 0)
fwrite(buffer, 1, nread, out);
}
Otherwise if you need to read it as strings use fgets and fputs in a loop:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *in = fopen(..., "r");
FILE *out = fopen(..., "w");
if (!in || !out)
return EXIT_FAILURE;
char line[256];
while (fgets(line, sizeof line, in) != NULL)
fputs(line, out);
}
Or as I mentioned, fgets to read a whole line, sscanf to parse out all three values, pass back to the calling function (together with a file read success status), and fprintf all three values at once:
#include <stdio.h>
#include <stdlib.h>
int read_values(FILE *in, int *values)
{
char line[256];
if (fgets(line, sizeof line, in) == NULL)
return 0; // Failure to read
if (sscanf(line("%x %x %x", &values[0], &values[1], &values[2]) != 3)
return 0; // Failure to parse
return 1; // Success
}
void write_values(FILE *out, int *values)
{
fprintf(out, "%02x %02x %02x\n", values[0], values[1], values[2]);
}
int main(void)
{
FILE *in = fopen(..., "r");
FILE *out = fopen(..., "w");
if (!in || !out)
return EXIT_FAILURE;
int values[3];
while (read_values(in, values))
write_values(out, values);
}
Note that I don't close the files. If your program is doing more after this, then they should really be closed. If the program just exits, then the system will close them for us.
Related
When I open and read a text file it works. However, when I open and try to read a binary file it stops at the first byte equal to 00.
For example, I try to read a png file, with the below first line in hexdump:
00000000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 |.PNG........IHDR|
But, I end up with a string that includes only these:
00000000 89 50 4e 47 0d 0a 1a 0a 0a |.PNG.....|
This is my code.
int main(int argc, char *argv[]) {
int num = 5;
char *filename;
char *myfile;
FILE *fp;
filename = argv[1];
if((fp = fopen(filename,"rb")) == NULL) {
printf("error open file\n");
exit(1);
}
myfile = (char *) malloc(num*sizeof(char));
if(myfile==NULL) {
printf("error memory\n");
exit(1);
}
int i=0;
while(!feof(fp)) {
fread(myfile+i,sizeof(char),1,fp);
if(i==num) {
myfile = (char *) realloc(myfile,sizeof(char)*(num*2));
if(myfile==NULL) {
printf("error memory\n");
exit(1);
}
num = num*2;
}
i++;
}
fclose(fp);
printf("%s\n",myfile);
return(0);
}
You're reading the entire file correctly. Instead, the problem lies here:
printf("%s\n",myfile);
printf doesn't know when your myfile string ends, and so assumes it's a cstring, which is terminated by the first null character.
You'd want to use fwrite instead:
fwrite(myfile, num, 1, stdout);
This isn't a complete working alternative, though -- from what I see in your code, num is likely to be higher unless your file's size is an exact power of two. You'd want to rewrite your program so that you know the exact size of the data you've read, and then replace num with that in fwrite.
I want to know why the code does not print the output.
What is the difference between binary and normal file modes?
#include<stdio.h>
#include <stdlib.h>
typedef struct book_details
{
char book_title[50];
int book_no;
float book_price;
}book_details;
int main()
{
book_details b;
FILE *fp;
fp = fopen("book_list.txt","w+");
if (fp == NULL)
printf("File not found");
fflush(stdin);
printf("Enter Book Title: \n");
gets(b.book_title);
printf("Enter Book ID Number: \n");
scanf("%d",&b.book_no);
printf("Enter Book Price: \n");
scanf("%f",&b.book_price);
fprintf(fp,"Here are the book details");
fwrite(&b,sizeof(b),1,fp);
while (fread(&b,sizeof(b),1,fp) > 0)
printf("%s %d %f\n",b.book_title,b.book_no,b.book_price);
fclose(fp);
}
What are the mistakes?
This happens because here same File Pointer fp is used for reading and writing.Your output file is a binary file so only fread() and fwrite() can be used here. You cannot use fprintf(fp,"Here are the book details"); in this case.This also cause error in reading.In such cases there are TWO solutions.
Using rewind().
Using the function rewind() we could rewind the File Pointer fp back to initial state to read the file.
Try this code :-
#include<stdio.h>
#include <stdlib.h>
typedef struct book_details
{
char book_title[50];
int book_no;
float book_price;
}book_details;
int main()
{
book_details b;
FILE *fp;
fp = fopen("book_list.txt","r+");
if (fp == NULL)
printf("File not found");
printf("Enter Book Title: \n");
gets(b.book_title);
printf("Enter Book ID Number: \n");
scanf("%d",&b.book_no);
printf("Enter Book Price: \n");
scanf("%f",&b.book_price); // removed fprintf();
fwrite(&b,sizeof(b),1,fp);
fflush(stdin);
rewind(fp); // Using rewind();
while (fread(&b,sizeof(b),1,fp) > 0)
printf("%s %d %f\n",b.book_title,b.book_no,b.book_price);
fclose(fp);
}
Using separate read and write FILE pointers.
Try this code:-
#include<stdio.h>
#include <stdlib.h>
typedef struct book_details
{
char book_title[50];
int book_no;
float book_price;
}book_details;
int main()
{
book_details b;
FILE *fpwrite,* fpread; // separate File pointers.
fpwrite = fopen("book_list.txt","w");
if (fpwrite == NULL)
printf("File not found");
printf("Enter Book Title: \n");
gets(b.book_title);
printf("Enter Book ID Number: \n");
scanf("%d",&b.book_no);
printf("Enter Book Price: \n");
scanf("%f",&b.book_price); // removed fprintf();
fflush(stdin);
fwrite(&b,sizeof(b),1,fpwrite);
fclose(fpwrite);
fpread = fopen("book_list.txt","r");
while (fread(&b,sizeof(b),1,fpread) > 0)
printf("%s %d %f\n",b.book_title,b.book_no,b.book_price);
fclose(fpread);
}
Separate File pointers are considered better since it improves Readability of source-code.
You have a large number of errors in your code. Most can be overcome by simply changing the way you write-to and then read-from your data file. There is no need to open the file in "w+" mode. You (1) write data, and then (2) read data. So simply open the file originally in append mode "a" (or "ab" to explicitly specify a binary write -- not required, but it does make what your are doing clear). Then close and open the file again to read.
When you write your book info out. Do NOT write "Here are the book details" to the file (it will break your read of struct from the file unless you offset the file position indicator beyond the unneeded text before starting your read of data).
Then close your file, you are done writing (validating the return of fclose after a write to catch any stream error not reported as you validated each write). Now simply open your file again in "r" (read) mode and loop over each struct read and outputting the values to the screen.
Rather than going over each error (including the use of gets() which from this day forward you will never never never do again) and the fflush (stdin) which is not supported by all OS's except for seekable streams, I have included comments inline below explaining how to handle the user-input, the write and the subsequent read. Aside from the workings of the code, the most important point I can make is to validate each and every input and output your code makes. That will save you untold amounts of time when things go wrong...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXT 50 /* if you need a constant #define one (or more) */
#define FNAM "book_list.txt"
#define DETAIL "Here are the book details"
typedef struct book_details {
char book_title[MAXT];
int book_no;
double book_price; /* don't use floating-point variables for currency */
} book_details_t; /* people get upset when you lose $ from rounding. */
int main (int argc, char **argv) { /* use arguments to pass info to main */
char *filename = argc > 1 ? argv[1] : FNAM; /* read from file given as */
book_details_t b = { .book_title = "" }; /* argv[1] or FNAM if none */
size_t len = 0; /* string length to aid with trimming '\n' */
FILE *fp;
fp = fopen (filename, "ab"); /* no need for "w+", "a" (append) will
* let you write new values to file,
* then reopen in "r" to read values
*/
if (fp == NULL) {
perror ("fopen-filename"); /* if the file isn't open, don't just */
return 1; /* output a msg, handle the error. */
}
// fflush(stdin); /* only valid on seekable streams or some OSs */
printf ("Enter Book Title: ");
fgets (b.book_title, MAXT, stdin); /* use fgets - NEVER ever gets */
len = strlen (b.book_title); /* get length of title */
if (len && b.book_title[len-1] == '\n') /* check len & ends with '\n' */
b.book_title[--len] = 0; /* replace '\n' with '\0' (0) */
else if (len == MAXT - 1) { /* otherwise title too long */
fprintf (stderr, "error: title too long.\n"); /* handle error */
return 1;
}
printf ("Enter Book ID Number: ");
if (scanf ("%d", &b.book_no) != 1) { /* must validate scanf return */
fprintf (stderr, "error: invalid book_no.\n"); /* every time! */
return 1;
}
printf("Enter Book Price: "); /* same thing for every input */
if (scanf ("%lf", &b.book_price) != 1) {
fprintf (stderr, "error: invalid book_price.\n");
return 1;
}
printf ("\n%s\n\n", DETAIL); /* don't write to file - will break read */
if (fwrite (&b, sizeof(b), 1, fp) != 1) { /* validate every write */
perror ("fwrite-b");
return 1;
}
if (fclose (fp) == EOF) { /* validate 'close after write' */
perror ("fclose after write");
return 1;
}
if ((fp = fopen (filename, "r")) == NULL) { /* validate open for read */
perror ("open for read");
return 1;
}
while (fread (&b, sizeof(b), 1, fp) > 0)
printf ("%s %d %f\n", b.book_title, b.book_no, b.book_price);
fclose(fp);
return 0;
}
(note: while C99+ will automatically return 0; at the end of main(), it is a good idea to include it)
Example Use/Output
$ ./bin/struct_read_after_write dat/struct_books.dat
Enter Book Title: Huck Finn
Enter Book ID Number: 10157
Enter Book Price: 19.99
Here are the book details
Huck Finn 10157 19.990000
Add next book:
$ ./bin/struct_read_after_write dat/struct_books.dat
Enter Book Title: Tom Sawyer
Enter Book ID Number: 10156
Enter Book Price: 22.95
Here are the book details
Huck Finn 10157 19.990000
Tom Sawyer 10156 22.950000
Check Binary File
$ hexdump -Cv dat/struct_books.dat
00000000 48 75 63 6b 20 46 69 6e 6e 00 00 00 00 00 00 00 |Huck Finn.......|
00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000030 00 00 00 00 ad 27 00 00 3d 0a d7 a3 70 fd 33 40 |.....'..=...p.3#|
00000040 54 6f 6d 20 53 61 77 79 65 72 00 00 00 00 00 00 |Tom Sawyer......|
00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000070 00 00 00 00 ac 27 00 00 33 33 33 33 33 f3 36 40 |.....'..33333.6#|
00000080
All good, but notice the wasted space from writing MAXT (50) chars for every title? (if you had not initialized b = { .book_title = "" }; what do you think would be in the file?) Later on you will want to serialize the data and only write the bytes containing data to the file, and precede the title by the number of characters to read -- but that is for another day.
Tweak to printf Formatting
You may also want to tidy up your output formatting a bit by including the field width modifier and left justification for the title, providing a fixed-width for the ID, and limiting the precision of the price to 2-places, e.g.
while (fread (&b, sizeof(b), 1, fp) > 0)
printf ("%-50s %6d $%.2f\n",
b.book_title, b.book_no, b.book_price);
Which would then produce tidier output of:
$ ./bin/struct_read_after_write dat/struct_books.dat
Enter Book Title: Validating All I/O
Enter Book ID Number: 101
Enter Book Price: 99.50
Here are the book details
Huck Finn 10157 $19.99
Tom Sawyer 10156 $22.95
My Dog with Fleas 10150 $9.99
Lucky Cats Have None 10151 $12.49
Fun with C 100 $59.21
Validating All I/O 101 $99.50
Look things over and let me know if you have further questions.
Iam having a problem with my school project. When i try to read in C program some hex data from a binary file i get some data filled with F's like this
00 64 61 56 03 00 00 00 09 00 00 00 73 00 00 00
6A 69 75 FFFFFFE8 FFFFFFD1 5B FFFFFF8C FFFFFF93 73 FFFFFFAF 19 FFFFFF87 FFFFFFC1 4D FFFFFFFD FFFFFF93
FFFFFFDF FFFFFFBE FFFFFFA0 09 FFFFFFBE FFFFFFD4 FFFFFFEB FFFFFFDE FFFFFFD3 FFFFFFF9 FFFFFFBD FFFFFFE6 FFFFFFFA FFFFFFAA 7E 29
FFFFFFD3 FFFFFFE2 13 FFFFFFA5 3F FFFFFFA0 3A FFFFFFB2 50 53 3B 12 FFFFFFA1 39 FFFFFFA6 FFFFFF82
FFFFFFF7
While the original file in hexdump looks like this:
0000000 6400 5661 0003 0000 0009 0000 0073 0000
0000010 696a e875 5bd1 938c af73 8719 4dc1 93fd
0000020 bedf 09a0 d4be deeb f9d3 e6bd aafa 297e
0000030 e2d3 a513 a03f b23a 5350 123b 39a1 82a6
0000040 00f7
Iam not quiet sure, why is this happening.
I tested it using this code:
int main()
{
char ch, file_name[25];
FILE *fp;
printf("Enter the name of file you wish to see\n");
fgets(file_name, sizeof(file_name), stdin);
file_name[strlen(file_name)-1] = 0x00;
fp = fopen(file_name,"rb"); // read binary mode
if( fp == NULL ) //error checking
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name);
int i;
while( ( ch = fgetc(fp) ) != EOF )
{
printf("%02X ",ch);
if( !(++i % 16) ) putc('\n', stdout);
}
fclose(fp);
putc('\n', stdout);
return 0;
}
You just tripped on the sign extension done by the processor when expanding a signed integer value to a larger integer type:
The type char is only one byte wide, while the printf() function expects int arguments (four bytes on your system). Since char is signed on your system, the other 24 bits are filled with copies of the sign bit, producing the FFFFFF pattern that you see in your output. Note that your output is correct, except for the FFFFFF-prefixes to all bytes that start with a hex digit greater or equal to 8.
You can avoid this by simply declaring ch as unsigned char. That will be zero extended to an int, and your output will be correct.
If you make ch an unsigned character you won't see the sign extension (the FFs).
It may not be a good idea to open a file as a binary file, and trying reading it by fgetc(), which deals with text files. Instead, you can use fread() to read binary files.
Here is my solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
int main()
{
char file_name[25];
puts("Enter the name of file you wish to see");
fgets(file_name, sizeof (file_name), stdin);
file_name[strlen(file_name) - 1] = '\0';
FILE *fp;
fp = fopen(file_name, "rb"); // read binary mode
if(!fp) //error checking
{
perror("fopen()");
exit(EXIT_FAILURE);
}
fseek (fp, 0, SEEK_END); // non-portable
long fSize = ftell(fp);
rewind(fp);
printf("The contents of %s file are :\n", file_name);
uint8_t *ch = malloc(fSize); // assuming CHAR_BIT == 8
fread(ch, 1, fSize, fp);
int i;
for(i = 0; i < fSize; i++)
{
printf("%.2x ", ch[i]);
if(!((i + 1) % 16))
putchar('\n');
}
fclose(fp);
putchar('\n');
return EXIT_SUCCESS;
}
This code has been tested on my machine, and its output is identical to that of hex dump. Feel free to ask me about the details.
make ch an unsigned char as it is treating the binary value as signed and the MSB as sign bit.
I am trying to read a file parsed from a command line argument 16 bytes at a time. I am storing the bytes in an unsigned char array. I am then trying to print the elements in their HEX format and then, if they are printable characters, I am printing them and if not I am printing a dot "." I also would like to print the offset of the byte from the beggining of the file on each new line but I want to get the rest working before I start working on that. The problem I am having is the file I am reading from is not printing so I don't think I am doing it right. I started out using fread() but I think I may be needing to use fseek() but I am unsure. If anyone could point me in the right direction or tell me if I'm doing anything wrong I'd appreciate it.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
int i, j;
unsigned char buffer[17];
fp = fopen(argv[1], "r");
while(!feof(fp))
{
while(fread(buffer, 16, 1, fp) !=0)
{
for(i=0; i < 16; i++)
{
printf("%X ", buffer[i]);
}
for(j = 0; j < 16; j++)
{
if(isprint(buffer[j]))
printf("%s", buffer[j]);
else
printf("." );
}
printf("\n");
}
}
fclose(fp);
return 0;
}
Expected output:
0001: 40 4F 28 37 0B B8 FF 9D 81 5E 2E 73 B5 CC 6A 70 #O(7.....^.s..jp
0010: 0F 30 9C 6E 7E F7 53 E0 C1 5E 9A 38 C5 02 F2 33 .0.n .S..^.8...3
EDIT: Fixed problem with suggestions. Was opening file in text mode instead of binary. Changed read mode to "rb" and code is working correctly now.
You need to open the file in binary mode, using fseek/ftell on a file in text mode gives erratic values.
So open the file with
fp = fopen(argv[1], "rb");
also good practice is to always check if a function succeeded by checking the return value, in this case if (fp != NULL) ...
Instead of reading 16 byte chunks read 1 byte 16 times, otherwise if the file length is not evenly dividable by 16 you miss the last bytes
if ( fp != NULL )
{
int read = 0;
while((read = fread(buffer, 1, 16, fp)) > 0)
{
for(i=0; i < read; i++)
{
...
}
fclose(fp);
}
IOW no need to for the outer while (feof(fp)) ...
in
printf("%s", buffer[j]);
you are not using the correct format specifier, buffer[j] is a character, not a string so write
printf("%c", buffer[j]);
EDIT: if you are not in some embedded environment with minimal stack reading 16 bytes at a time is not so effective, you can just as might read 2k or some bigger size to read faster.
I have a file, such as the following (in hex).
1F 00 48 3A 18 00 00 00 53 00 70 00 6F 00 75 00
73 00 65 00 5F 00 61 00 7A 00 61 00 6D 00 00 00
I am trying to read the binary data from the file and output it as text. My current code is as follows:
#include<stdlib.h>
#include<stdio.h>
#include<iostream.h>
int main()
{
FILE *pFile, *tempFile;
pFile = fopen("C:\\wab files\\Admin.wab", "rb");
if(pFile == NULL)
{
fputs("file error", stderr);
exit(1);
}
tempFile = fopen("C:\\myfile.text","wb");
if(tempFile == NULL)
{
fputs("file not open", stderr);
exit(2);
}
int Contact_Id, Id_Size, Data_Info=0;
fread(&Contact_Id, 1, 4, pFile);
fread(&Id_Size, 1, 4, pFile);
Data_Info = Id_Size;
char* Main_buffer = (char*)malloc(Data_Info*sizeof(Data_Info));
fread(Main_buffer, 1, Id_Size, pFile);
const wchar_t* src = (unsigned short *) Main_buffer;
wcstombs ((char*) Main_buffer, src, Data_Info );
fwrite(Main_buffer, 1, Data_Info, tempFile);
free(Main_buffer);
return 0;
}
The output text file containts follow:
Spouse_azam _ a z a m
Why is _ a z a m shown in text file? I wants to write only Spouse_azam.
First of all this is wrong:
char* Main_buffer = (char*)malloc(Data_Info*sizeof(Data_Info));
It should be
char* Main_buffer = (char*)malloc(Data_Info*sizeof(char));
Second, you are using the same buffer (Main_buffer) for both source and destination. The result is that the first part of the buffer is overwritten with the converted string, but the rest of the buffer is left untouched. You have 24 bytes initially in the buffer (if you allocate correctly), and the first 11 are overwritten, but the other 13 are left there.
There are two options: use a second buffer for the multi-byte string, or use the return value of wcstombs to know how many bytes the result string has:
int bytes = wcstombs ((char*) Main_buffer, src, Data_Info );
fwrite(Main_buffer, 1, bytes, tempFile);