I need to read a line from a text file by character by character. Then
assign the words in that line, which are separated by spaces in to
separate strings. Pl. tell me how to code this from c++.
Thank you for all replied to me.
I am an old type programmer used Turbo Pascal and C 10 years back. Now
I need a simple word extracting program. I thought reading char by
char is easy, but it is OK, if I can read the word directly. So I
tried as follows at the first time and it is not working.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
FILE *fp; /* file pointer */
char ch;
char s1[80];
strcpy(s1,"");
/* open file for input */
if ((fp = fopen("Example.txt", "r"))==NULL) {
printf("Cannot open file \n");
exit(1);
}
/* look for character */
while ((ch = getc(fp)) !=EOF )
{
while (ch != " ")
{ strcat (s1, ch);
cout << ch;
cout << s1;
}
}
fclose(fp);
cout << s1;
cin >> ch; /* to check the o/p */
}