| | |
Read file using fget
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 16
Reputation:
Solved Threads: 0
I have the following in a text file
How old are you?:I am 1 year old.
Basically, the ":" is the delimiter. "How old are you?" is the question and "I am 1 year old" is what i will reply when i get the question .
There is basically 2 fields in this. I know in C++ i can just do a getline(cin, question, answer,
specifying the delimiter.
however, i cant do that in c? is there a better way to do it? store the first field in one string variable and 2nd field in another stirng variable?
How old are you?:I am 1 year old.
Basically, the ":" is the delimiter. "How old are you?" is the question and "I am 1 year old" is what i will reply when i get the question .
There is basically 2 fields in this. I know in C++ i can just do a getline(cin, question, answer,
specifying the delimiter. however, i cant do that in c? is there a better way to do it? store the first field in one string variable and 2nd field in another stirng variable?
•
•
Join Date: Oct 2008
Posts: 16
Reputation:
Solved Threads: 0
I've tried using fgets to read whole line.
However, it only reads the first line and end from there. Did i forget to put a loop or something?
However, it only reads the first line and end from there. Did i forget to put a loop or something?
•
•
•
•
#include <stdio.h>
int main()
{
FILE * pFile;
char mystring [300];
pFile = fopen ("question.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
while(fgets (mystring , 300 , pFile)) {
fgets (mystring , 300 , pFile);
puts (mystring);
fclose (pFile);
system("pause");
}
return 0;
}
![]() |
Similar Threads
- using a process to run another one (C)
- C: - reading in from text (C++)
- how to test a software program on linux (C)
- Capture text between tags (PHP)
Other Threads in the C Forum
- Previous Thread: Odd result in fscanf
- Next Thread: need help: shuffle 2-dimensional array
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm 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 iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue multi mysql odf open opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition research scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard string strings suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






