Here is my file text file data

Tree Bush
Apple Mango
Animal Dog
Animal2 Cat

#include <stdio.h>
#include<conio.h>
#include<string>
#include<iostream>
#include<fstream>
using namespace std; 

int main()
{
   FILE * pFile;
   char mystring [100];
   char mystring2[100];
   char ch;
    
  int i=0,j=0;
                
   pFile = fopen ("c:\\List.txt" , "r");
   if (pFile == NULL) perror ("Error opening file");
   else
   {
      
          while(!feof(pFile)) 
          { 
                     
             while(getc(pFile)!=' ');
             {
              
               fgets (mystring , 20 , pFile);
               printf("\n");
                         
               puts (mystring);
              
                          
             }             
        
          }   
        
    
    }
   
    fclose (pFile);
    getch();
  
}

i get output
Bush
Mango
Dog
Cat

i m not getting 1st string in line ..
that means --->Tree
Apple
Animal
Animal2
how to get this string??
i want each string individually ??how to get this??

Note the semicolon next to the while loop,

while(getc(pFile)!=' ');

Thanks to that, your while continues to run and skip all the characters on the line until it finds a ' ' <space>.

Just remove that semincolon and try.

Even then output might not be what you desire, getc returns the character currently pointed and also advances the position indicator by 1. so it'll always stub the first char from the output. since you're already checking for the 'eof' you dont need this extra check using getc. You can completely remove that while loop.

what was wrong with 'cout' though?

Here is my file text file data

Tree Bush
Apple Mango
Animal Dog
Animal2 Cat

i want Tree in 1 string and Bush in 2nd string so i m trying to collect them bCoz they separated by space
How to extract this from text file as a string??

If you want to make this in c++, you could use filestreams and getline() for this assignment. The functions you're currently using are to 'c' for my taste.
I'll give you a simple demo-app:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream in;
    in.open("yourfile.txt");
    if (!in.is_open()) 
    {
        cout << "Can't open file\n";
        return 1;
    }
    string word;
    while (getline(in, word, ' '))
        cout << word << "\n";

    return 0;
}

also read this about feof()

commented: good help +4

i m modifying my problem definition here :--

Here is my file text file data

Tree Bush
Apple Mango
Animal Dog
Animal2 Cat

i want Tree in str1 ie string
& Bush in str2 ie string

string str1,str2;

and send it to function
like Myfun(str1,str2);

But problem is How to store this string into str1 and str2???

This is a modified version of niek_e's code

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    ifstream in;
    in.open("c:\\yourfile.txt");
    if (!in.is_open()) 
    {
        cout << "Can't open file\n";
        return 1;
    }
    
    string line;

    // read one line at a time
    while (getline(in, line))
    {
        stringstream sstrm(line);
        
        string word1, word2;

        if(sstrm >> word1 >> word2)
        {
            // two words extracted ...
            MyFun(word1, word2);
        }
    }

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.