| | |
Reading and Writing file
![]() |
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
Solved Threads: 0
Can anyone help me...i have a file modt.txt and i want to save the content in modt2.txt but it seems my code is not working. Moreover,
if i have the content of the 1st file Have a nice day. i want to save it to the 2nd file without double space it should be Have a nice day. I have this as my code:
if i have the content of the 1st file Have a nice day. i want to save it to the 2nd file without double space it should be Have a nice day. I have this as my code:
C Syntax (Toggle Plain Text)
#include <stdio.h> void copyf(char *fname1, char *fname2){ FILE *fp1,*fp2; int x; if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL)) { printf("error:unable to open the filed\n"); exit(1); } while((x=getc(fp1))!=EOF) putc(x,fp2); fclose(fp1); fclose(fp1); } main(){ copyf("modt.txt","modt2.txt"); }
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
Solved Threads: 0
im sory the first txt file should have a content of "Have_ _ a nice day" two spaces then i have to save it to the 2nd file and should only have one spcae "Have_a nice day"...
C Syntax (Toggle Plain Text)
while((x=getc(fp1))!=EOF){ if ((unsigned char) x != ' ' || spaceOcured != 1) putc(x,fp2); if ((unsigned char) x == ' '){ spaceOcured = 1; } else{ spaceOcured = 0; } }
Last edited by andor; Oct 2nd, 2006 at 3:51 am.
If you want to win, you must not loose (Alan Ford)
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
Solved Threads: 0
•
•
•
•
Of course you must to define spaceOcured at the begining of copyf function, for example to char and intialise it to zero.C Syntax (Toggle Plain Text)
while((x=getc(fp1))!=EOF){ if ((unsigned char) x != ' ' || spaceOcured != 1) putc(x,fp2); if ((unsigned char) x == ' '){ spaceOcured = 1; } else{ spaceOcured = 0; } }
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
Solved Threads: 0
•
•
•
•
i got it i initialize spaceOccured as char
char spaceOcured=0;
with this code whats wrong?:
C Syntax (Toggle Plain Text)
#include <stdio.h> char spaceOcured; void copyf(char *fname1, char *fname2){ FILE *fp1,*fp2; int x; spaceOcured=0; if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL)) { printf("error:unable to open the filed\n"); exit(1); } while((x=getc(fp1))!=EOF){ if ((unsigned char) x != ' ' || spaceOcured != 1) putc(x,fp2); if ((unsigned char) x == ' '){ spaceOcured = 1; } else{ spaceOcured = 0; } } fclose(fp1); fclose(fp1); } main(){ copyf("d:\modt.txt","d:\modt2.txt"); getch(); }
Hm are you shore?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> void copyf(char *fname1, char *fname2){ FILE *fp1,*fp2; int x; char spaceOcured = 0; if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL)){ printf("error:unable to open the filed\n"); exit(1); } while((x=getc(fp1))!=EOF){ if ((unsigned char) x != ' ' || spaceOcured != 1) putc(x,fp2); if ((unsigned char) x == ' '){ spaceOcured = 1; } else{ spaceOcured = 0; } } fclose(fp1); fclose(fp1); } int main(){ copyf("modt.txt","modt2.txt"); return 0; }
If you want to win, you must not loose (Alan Ford)
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
Solved Threads: 0
•
•
•
•
Hm are you shore?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> void copyf(char *fname1, char *fname2){ FILE *fp1,*fp2; int x; char spaceOcured = 0; if (((fp1=fopen(fname1,"r"))==NULL)||((fp2=fopen(fname2,"w"))==NULL)){ printf("error:unable to open the filed\n"); exit(1); } while((x=getc(fp1))!=EOF){ if ((unsigned char) x != ' ' || spaceOcured != 1) putc(x,fp2); if ((unsigned char) x == ' '){ spaceOcured = 1; } else{ spaceOcured = 0; } } fclose(fp1); fclose(fp1); } int main(){ copyf("modt.txt","modt2.txt"); return 0; }
The only diffrence is the while loop and the declaration of spaceOcured. There is no sense that mod2 is not created. Create a new dir and copy the c file and the modt.txt. Try again. It should create the file modt2.txt. Do you heve this error message
error:unable to open the filed? If U still have problem comment the while loop and leave only the fopen functions and see the result. Last edited by andor; Oct 2nd, 2006 at 5:03 am.
If you want to win, you must not loose (Alan Ford)
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- Reading numbers from a text file (Java)
- C++ Reading from a text file (C++)
- What errors can occur while reading a file? (C)
- Reading specific line in a file. / Searching (C++)
- Read/write to same file > once, Help (C++)
- Reading binary data from a file and writing it (Visual Basic 4 / 5 / 6)
- writing to a file (Java)
- file input and output (C)
Other Threads in the C Forum
- Previous Thread: Binary Search of an array of three strings
- Next Thread: passing strings
Views: 3457 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for C
api array arrays behaviour binary binarysearch c++ calculator changingto char character code coke command conversion convert copyimagefile creafecopyofanytypeoffileinc database decimal directory dude dynamic error exec factorial fgetc fgets file fork frequency function functions givemetehcodez grade graphics homework i/o incrementoperators input insert int integer lazy line linked linkedlist linux list lists loop malloc matrix measuring memory mysql no-code no-effort open opensource operator output path pointer pointers printf problem process program programming question read recursion recursive recv reverse scanf scripting send sms_speak socketprograming spoonfeeding stdout steganography string strings strtok structures student suggestions syntax system turbo-c turboc undefined unix user variable whileloop windows





