| | |
Write to a file the first word in each line from another file
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 82
Reputation:
Solved Threads: 0
Hey
I have a file named filetext.txt with the text:
One word
Two words
Three words
Four words
and the program should read this filetext.txt and output to another file called filecopied.txt this text:
One
Two
Three
Four
I have this so far:
It doesnt write anything to the file so how can I solve it? Id like to use fgets, fprintf, and sscanf. Thanks.
I have a file named filetext.txt with the text:
One word
Two words
Three words
Four words
and the program should read this filetext.txt and output to another file called filecopied.txt this text:
One
Two
Three
Four
I have this so far:
C Syntax (Toggle Plain Text)
#include <assert.h> #include <complex.h> #include <ctype.h> #include <errno.h> #include <fenv.h> #include <float.h> #include <inttypes.h> #include <iso646.h> #include <limits.h> #include <locale.h> #include <math.h> #include <setjmp.h> #include <signal.h> #include <stdarg.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <wchar.h> #include <wctype.h> int main() { FILE *filetext FILE *filecopied; char text[50]; filetext=fopen("filetext.txt","r"); filecopied=fopen("filecopied.txt","w"); if ((filetext==NULL) || (filecopied==NULL)) { printf ("A file cannot be opened"); return 1; } // fgets(text,50,filetext); //sscanf("%s ",text); sscanf("%s\n",fgets(text,50,filetext)); //sscanf("%s\n",fgets(text,50,filetext)); fprintf(filecopied,"%s",text); fclose(filecopied); fclose(filetext); return 0; }
It doesnt write anything to the file so how can I solve it? Id like to use fgets, fprintf, and sscanf. Thanks.
Last edited by riahc3; Oct 27th, 2009 at 3:04 pm.
0
#2 Oct 27th, 2009
int main()
{
FILE *filetext;
FILE *filecopied;
char text[50];
filetext=fopen("filetext.txt","r");
filecopied=fopen("filecopied.txt","w");
if ( (filetext==NULL) || (filecopied==NULL) )
{
printf ("A file cannot be opened");
return 1;
}
while ( fgets(text, sizeof text, filetext) )
{
char first[10];
if ( sscanf(text, "%9s", first) == 1 )
{
fprintf(filecopied, "%s\n", first);
}
}
fclose(filecopied);
fclose(filetext);
return 0;
} "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
0
#3 Oct 27th, 2009
•
•
•
•
C Syntax (Toggle Plain Text)
#include <assert.h> #include <complex.h> #include <ctype.h> #include <errno.h> #include <fenv.h> #include <float.h> #include <inttypes.h> #include <iso646.h> #include <limits.h> #include <locale.h> #include <math.h> #include <setjmp.h> #include <signal.h> #include <stdarg.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <wchar.h> #include <wctype.h>
•
•
•
•
It doesnt write anything to the file so how can I solve it?
C Syntax (Toggle Plain Text)
char line[512]; char word[512]; while (fgets(line, sizeof line, filetext)) { if (sscanf(line, "%511s", word) == 1) { fprintf(filecopied, "%s\n", word); } }
Last edited by Tom Gunn; Oct 27th, 2009 at 3:30 pm. Reason: Saving myself from a nitpick.
-Tommy (For Great Justice!) Gunn
0
#6 Oct 29th, 2009
•
•
•
•
Id like to use fgets, fprintf, and sscanf.
•
•
•
•
I want it to read/write the first letters then when it detects the first space (" ") and when it detects a "\0", it should just to the next line again the first space and again a "\0" until it reaches EOF
You can skip using sscanf() entirely by searching for the first whitespace in the line and save the memory for the second buffer:
C Syntax (Toggle Plain Text)
char line[512]; while (fgets(line, sizeof line, filetext)) { size_t x = 0; while (line[x] && !isspace(line[x])) putc(line[x], filecopied); putc('\n', filecopied); }
-Tommy (For Great Justice!) Gunn
![]() |
Similar Threads
- how to write and read array record into file?? (Pascal and Delphi)
- opening a excel from word macro by getting the file name from word at runtime (Visual Basic 4 / 5 / 6)
- Write int and string to binary file woes (C++)
- help finding line in file! (C++)
- Help: importing text file to a word doc using array (Java)
- Deleting a line from a file (PHP)
- Find a word in a file (Python)
- Reading specific line in a file. / Searching (C++)
- Searching a file for a string (C++)
Other Threads in the C Forum
- Previous Thread: Figuring out redirection of a child process to a file
- Next Thread: while condition won't work properly against \n
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop initialization input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list lists locate looping lowest matrix meter microsoft number oddnumber opendocumentformat openwebfoundation overwrite owf pdf pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






