| | |
Only partially printing a string
![]() |
•
•
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 |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






