vkca
May 13, 2012, 10:43am
1
i am reading and writing to a a file in C language. the input file is described as follows
111 aaa descr1
222 bbb descr2
333 ccc <SPACE> {6 spaces are left after ccc i.e in 3rd column}
444 ddd descr4
when i read and write to a file, the space is not coming in the output file.
111aaadescr1222bbbdescr2333ccc444ddddescr4
Expected output:
111aaadescr1222bbbdescr2333ccc 444ddddescr4
Can somebody help me in modifying the code?
Sure. Please show us the code.
vkca
May 13, 2012, 11:34am
3
#include<stdio.h>
#include<conio.h>
struct buffer {
char data1[255];
char data2[255];
char data3[255];
};
int main(int argc, char *argv[]){
struct buffer dat;
char *line = NULL;
FILE *inFilePtr;
FILE *outFilePtr;
inFilePtr = fopen("test.txt", "r+");
outFilePtr = fopen("out.txt", "w+");
while (!feof(inFilePtr)){
fscanf(inFilePtr, "%s", &line);
printf("%s", &line);
fprintf(outFilePtr,"%s", &line);
}
fclose(inFilePtr);
fclose(outFilePtr);
getch();
return 0;
}
fscanf skips over blanks. Experiment with this
char tmp[80]={0x0};
while fgets(tmp, sizeof(tmp), inFilePtr)
{
fprintf(outFilePtr, "%s", tmp);
}
vkca
May 13, 2012, 11:54am
5
hi Jim,
fgets is not working. the output is same as the input
111 aaa descr1
222 bbb descr2
333 ccc <SPACE> {6 spaces are left after ccc i.e in 3rd column}
444 ddd descr4
That's exactly what you asked for -- spaces in the output, just like the input.
If that's not what you want now, what do you want instead?
vkca
May 14, 2012, 11:47am
7
Mr Corona688,
please read my first thread..... u will get to know what i am expecting!
i asked the output to be in this format
111aaadescr1222bbbdescr2333ccc 444ddddescr4
but my program output is coming like
111aaadescr1222bbbdescr2333ccc444ddddescr4
I read the thread. You specifically said, in the post before this one, that you had spaces in your output.
jim is correct in that fgets does not delete spaces. If you're losing spaces, you're either deleting the spaces or not using fgets. Please show your complete program.