| | |
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 |
Tag cloud for C
adobe ansi api array arrays asterisks bash binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






