files and fgets problem
This is what the question is stating:
Write a program that inputs a line of text using function fgets() into a char array s[90], then it outputs the line converted to uppercase.
For function fgets to be used, am i correct if I understand that the user doesn't have to input anything but the text should be get from the file only?
Keeping with the same function, I found that this is how the function is used. fgets ( char * str, int num, FILE * stream ); Am I correct if I understand that: char *str => the array of characters where the data from the text file is to be stored int num => how big the array is FILE * stream => the file where data is to be get from.
?
Related Article: problem with files
is a C discussion thread by bufospro that has 1 reply and was last updated 1 year ago.
terence193
Junior Poster in Training
57 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0
(since I couldn't edit post)
I have attempted this so far, but the code isn't compiling since it is saying that data types that need to be upper-ed aren't the same
#include <stdio.h>
#include<ctype.h>
int main()
{
char line[80],ch;
fgets(line,80,stdin);
ch=toupper(line[80]);
printf("your quote was : %s",ch);
fflush(stdin);
getchar();
return 0;
}
terence193
Junior Poster in Training
57 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0
While fgets() suggests reading from a file, you can pass stdin as the stream for user input.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 53
While fgets() suggests reading from a file, you can pass stdin as the stream for user input.
Yes yes , I got that by some research on google but since the question asks to use that, I used that and made matters more simple since I haven't studied files well.
I tried the program (in the post above), but the problem now seems to be the toupper. I can't get it how toupper needs a type integer, when integers don't really have an uppercase , as long as I recall well :P
terence193
Junior Poster in Training
57 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0
A letter is just an integer value. 41 = '1', 65 = 'A', 97 = 'a'. It all depends how you display the value, as a character or as an integer.
1) Do not edit a post after someone responds to it. It makes their response seem wrong.
2) fflush(stdin) is wrong -- see this
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36
You are trying to print a character as a string:
printf("your quote was : %s",ch);
I'm not sure why toupper doesn't work with my IDE but try this:
printf("your quote was : %s",toupper(line));
mikrosfoititis
Junior Poster in Training
74 posts since Nov 2011
Reputation Points: 18
Solved Threads: 11
Skill Endorsements: 1
You are trying to print a character as a string:
printf("your quote was : %s",ch);
I'm not sure why toupper doesn't work with my IDE but try this:
printf("your quote was : %s",toupper(line));
toupper() returns a single character...
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 53
@mikrosfoititis: These errors where shown.
expected 'int' but argument is of type 'char *'
format '%s' expects type 'char *', but argument 2 has type 'int'
@Narue: Is there a way I can use toupper() with a string then?
terence193
Junior Poster in Training
57 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0
@Narue: Is there a way I can use toupper() with a string then?
Yes, the way you do everything with each character of a string: use a LOOP!
int i;
for (i = 0; line[i] != '\0'; i++)
line[i] = toupper(line[i]);
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 53
How I wish my lecturer's notes and explanations where this way, including at least a guide that a loop should be used rather than a very unhelpful
"#include<ctype.h>
eg char ch=toupper(variable-name);"
Perhaps I should have thought it out anyways.
Thanks :]
terence193
Junior Poster in Training
57 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
Narue,
WaltP
and
mikrosfoititis oh yeah, silly me.
So toupper() works on only one chaarcter!
mikrosfoititis
Junior Poster in Training
74 posts since Nov 2011
Reputation Points: 18
Solved Threads: 11
Skill Endorsements: 1
Perhaps I should have thought it out anyways.
That's usually the best solution when learning. You remember more after figuring out a solution rather than letting someone else give it to you.
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36