| | |
Only partially printing a string
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2008
Posts: 40
Reputation:
Solved Threads: 0
Hi,
I am working on a program that opens a file, reads the text, converts the text to uppercase, and then displays it on the screen. If I write the program to just type in a string and convert it, it works fine. If, however, I write it to open and read a fine it doesn't work. In my test file I typed, "This is a test," but the program only prints "THIS." Can you please help me figure out why the rest of the sting isn't being printed?
I am working on a program that opens a file, reads the text, converts the text to uppercase, and then displays it on the screen. If I write the program to just type in a string and convert it, it works fine. If, however, I write it to open and read a fine it doesn't work. In my test file I typed, "This is a test," but the program only prints "THIS." Can you please help me figure out why the rest of the sting isn't being printed?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <ctype.h> int main(void) { char str[300]; int i; FILE *inFile; char fileName[25]; //printf("Enter a string: "); //gets(str); printf("\nEnter a file name: "); gets(fileName); inFile = fopen(fileName, "r"); /*This opens the file */ if (inFile == NULL) /*Checks that the file exists*/ { printf("\nThe file %s was not opened successfully.", fileName); printf("\nPlease check that you entered the file name correctly.\n"); exit(1); } while (fscanf(inFile, "%s", str) !=EOF) /*Reads and displays the file*/ fclose(inFile); for( i = 0; str[ i ]; i++) str[ i ] = toupper( str[ i ] ); printf("%s\n", str); /* uppercase string */ return 0; }
use fgets() -- never ever gets() -- to read the entire line from a file or from the keyboard.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
That's true -- it doesn't take one parameter. You need to look up the functions you want to use either in your compiler's printed docs (if they still have them) or on the net (ever hear of google?). Don't just toss functions on a *.cpp file and expect the program to compiler.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Well, then the reasonable thing to do first would be to learn how to understand the documentation. Because you are not going to get very far by depending on others to tell you how you should program. Expecting that someone would be always willing to hold your hand is not going to work neither.
Last edited by Aia; Jan 15th, 2009 at 6:28 pm.
One such tutorial maybe?
http://www.daniweb.com/tutorials/tutorial45806.html
http://www.daniweb.com/tutorials/tutorial45806.html
*Voted best profile in the world*
•
•
Join Date: Jan 2009
Posts: 44
Reputation:
Solved Threads: 5
while (fscanf(inFile, "%s", str) !=EOF) /*Reads and displays the file*/
fclose(inFile); //Why are you closing the file in mid of everything
for( i = 0; str[ i ]; i++)
str[ i ] = toupper( str[ i ] );
printf("%s\n", str); /* uppercase string */
return 0;
}if you close the file just after reading the first line, then its quite obvious that the buffer 'str' will contain only the first line from the file and the next call to fscanf will fail and hence the while loop will terminate after 1st call to fscanf and hence no further line from the file will be read..
You should close the file after all the processing is done...hope you understood
•
•
Join Date: Jan 2009
Posts: 44
Reputation:
Solved Threads: 5
Here is the prototype solution...
Hope this helps..
C Syntax (Toggle Plain Text)
while (fscanf(inFile, "%s", str) !=EOF) /*Reads and displays the file*/ { for( i = 0; str[ i ]; i++) str[ i ] = toupper( str[ i ] ); printf("%s\n", str); /* uppercase string */ } fclose(inFile);
Hope this helps..
![]() |
Other Threads in the C Forum
- Previous Thread: reading an integer from file in c
- Next Thread: How do I calculate the closest number to a number?
| Thread Tools | Search this Thread |
adobe api array arrays bash binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue multi mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user variable voidmain() win32api windows.h






