| | |
Retrieving a paragraph instead of a single line of text
![]() |
•
•
Join Date: Sep 2008
Posts: 2
Reputation:
Solved Threads: 0
Hi, I was practicing for our hands on exams tomorrow when I encountered a problem in my code. Here is a part of my code:
The problem is in this part:
This is the part that retrieves the user input which will be copied to a text file. However, this only allows to retrieve a single line of text instead of retrieving everything that was entered by the user.
For example, the user enters:
---------------------------------------------------
Let's see... So far, nothing good happened to me. I look back and all I see were a bunch mistakes that I have committed. All I see were things I knew I shouldn't have done but...
<enter>
Am I still in control of my life? Sometimes, I think don't have the authority anymore. Why did I end up with something that only takes half of my interest (BSMMA)? And why did I end up here when I was...
---------------------------------------------------
The program will only retrieve the part after the user presses enter for the next paragraph.
How do I fix this code so that it will not only retrieve the user input per line but everything that the user entered for the entry tag?
Thanks for the help.
C Syntax (Toggle Plain Text)
void interactive(){ //if argument is 0, program will enter the interactive mode. int choice; int year, month, day; char div[40]; char tags[WORD_COUNT]; char entry[WORD_COUNT]; printf("\t\t....................................................\n"); printf("\t\t What do you wish to do?\n"); printf("\n\n\t\t[1]\tAdd new entry\n\t\t[2]\tEdit entry\n\t\t[3]\tList Entry\n\t\t[4]\tQuit\n"); scanf("%d", &choice); printf("\nYour choice is: %d\n\n", choice); inFile = fopen("journal.txt", "a"); if(choice == 1){ printf("Enter your entry's date [yy,mm,dd format]: "); scanf("%d, %d, %d", &year, &month, &day); fprintf(inFile, "------------------------------------------------------------------------------\n\n%s", div); fprintf(inFile, "Date: %d, %d, %d\n", year, month, day); printf("[Tags: Press ^Z to proceed] Enter your post's keywords: "); while(fgets(tags, WORD_COUNT, stdin)){ len = strlen(tags); tags[len-1] = '\0'; } fprintf(inFile, "\nTags: %s", tags); printf("\n[Press ^Z to post] Your entry:\n\n"); while(fgets(entry, WORD_COUNT, stdin)){ len2 = strlen(entry); entry[len2-1] = '\0'; } fprintf(inFile, "\n\n>START OF ENTRY\n\n\n %s \n\n\n>END OF ENTRY\n\n\n", entry); fclose(inFile); } if(choice == 4){ printf("Thank you for using ............................. \n\n"); printf("Program will now terminate\n"); exit(0); } if (choice > 4){ printf("Your choice is not in the menu. Program will now terminate\n\n"); } }
The problem is in this part:
C Syntax (Toggle Plain Text)
while(fgets(entry, WORD_COUNT, stdin)){ len2 = strlen(entry); entry[len2-1] = '\0'; }
This is the part that retrieves the user input which will be copied to a text file. However, this only allows to retrieve a single line of text instead of retrieving everything that was entered by the user.
For example, the user enters:
---------------------------------------------------
Let's see... So far, nothing good happened to me. I look back and all I see were a bunch mistakes that I have committed. All I see were things I knew I shouldn't have done but...
<enter>
Am I still in control of my life? Sometimes, I think don't have the authority anymore. Why did I end up with something that only takes half of my interest (BSMMA)? And why did I end up here when I was...
---------------------------------------------------
The program will only retrieve the part after the user presses enter for the next paragraph.
How do I fix this code so that it will not only retrieve the user input per line but everything that the user entered for the entry tag?
Thanks for the help.
Last edited by hikaru1239; Oct 23rd, 2008 at 1:45 pm.
•
•
Join Date: Sep 2008
Posts: 1,563
Reputation:
Solved Threads: 196
You would keep a pointer to the String that you wish to store it in and concatenate your 'new' user entry onto whatever the String's current contents are. OR you could make the function 'return' whatever the user entered as a String, and concatenate that entry with whats stored in the current String.
I haven't coded in C recently, but those are the only two ways you can do this. The pseudocode for the second way would look like this:
int main(){
String allTextEntered;
String currentEntry = interactive();
allTextEntered + currentEntry; //PSEUDOCODE - WONT WORK AS TYPED. CONCEPT WORKS.
}
String interactive(){
//code to get text from the user
return userEntry;
}
I haven't coded in C recently, but those are the only two ways you can do this. The pseudocode for the second way would look like this:
int main(){
String allTextEntered;
String currentEntry = interactive();
allTextEntered + currentEntry; //PSEUDOCODE - WONT WORK AS TYPED. CONCEPT WORKS.
}
String interactive(){
//code to get text from the user
return userEntry;
}
Last edited by BestJewSinceJC; Oct 23rd, 2008 at 7:00 pm.
The standard input (stdin) is buffered. That's why you can write and nothing happens until you give the signal to go ahead and read. That occurs when you press ENTER/RETURN.
fgets() can only read up to n-1 of whatever size of buffer you have set apart for it. In this case WORD_COUNT. That portion of read needs to be store in some memory or next time when while() goes through, variable entry will be over written. After fgets() can not read anything else, entry will hold the last portion of input available.
If you would know before hand how much is there in the input you could create an array big enough to hold everything; by copying and concatenating each read, using strcpy() and strcat().
The risk is that more input could be entered than this array memory can hold, creating a buffer overrun.
The other alternative is learning about dynamic memory. Which it is somehow out of the reach of a quick example.
fgets() can only read up to n-1 of whatever size of buffer you have set apart for it. In this case WORD_COUNT. That portion of read needs to be store in some memory or next time when while() goes through, variable entry will be over written. After fgets() can not read anything else, entry will hold the last portion of input available.
If you would know before hand how much is there in the input you could create an array big enough to hold everything; by copying and concatenating each read, using strcpy() and strcat().
The risk is that more input could be entered than this array memory can hold, creating a buffer overrun.
The other alternative is learning about dynamic memory. Which it is somehow out of the reach of a quick example.
![]() |
Other Threads in the C Forum
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






