| | |
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
Views: 1658 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
* api append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax database directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function functions getlogicaldrivestrin givemetehcodez global graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked reversing scheduling segmentationfault send single socketprogramming spoonfeeding standard strchr string student suggestions system test testautomation testing unix urboc user whythiscodecausesegmentationfault win32api windowsapi






