Hey there ,
I have a problem about saving information, which is read from a text file, to an char array.I have to read the file character by character.I could't use getline() function and string, I have to use get() function.There is a sample of the all data :

" Tsutsumi-Ishii,Y. Response of heat shock element within the human HSP70 promoter to mutated p53 genes 1995 Cell Growth Differ 6
Fraser,M. Akt promotes cisplatin resistance in human ovarian cancer cells through inhibition of p53 phosphorylation and nuclear function 2008 Int J Cancer 122
"
We have authors' names and some information about them.The purpose of this program ,
user is going to enter name of the author and we will show author's information on the screen.

I tried save the whole information in an char array like :

while(!fin.eof())
{ int count =0;
  for(int i=0 ; i<5000 ; i++)
   {   for(int j=0;j<1000; j++)
        { fin.get(array[i][j])

          if(array[i][j] == '\n') break;             
          else continue;
        }
   }
}

However this does not store the data in array , when I tried to write array on screen out of the while , it crashed.I think it is about get method .I tried to call that method by reference, but did not work.
Finally I wrote a specific function about calling by reference , it is also not working.
I do not know where the problem. When my program is run, there is an error occurred , "main.exe has stopped working".

Here is my final code:

#include <iostream>
#include <fstream>
using namespace std;
char myfunc ( ifstream &fin_ref , char &c);
int main()
{
      ifstream fin;
      // open the file
      fin.open("dataset.txt");

      char array[5000][1000];
      char input[10];
       // if smth goes wrong about openning data
           if(fin.fail())
           cout<<"error"<<endl; return 0;

           // take the author name
           cout<<"enter the first 3 char of your author name please \n";
           for(int t=0 ; t<10;t++)
           cin>>input[t];

         // set the whole data in multi dim. array.
         char array1[1000];
        char x;
        for(int i=0 ; i<5000 ; i++)
          {
              for(int j =0 ; j<2000, array[i][j] != '#'; j++)
                {
                     //fin.get((char *)& array , sizeof(array));
                     array[i][j] = myfunc(fin , x);
                }
          }

         // close the file
         fin.close();

 int count =0; // check how many char is going to be equal.
     for(int i=0 ; i<5000 ; i++)
          {
              for(int j =0 ; j<10; j++)
                {  if(input[j] == array[i][j])
                       count = count +1;
                }
                if(count >= 3) // if more than  or equals to tree char than we write the author info
                   for(int k =0 ; k<2000; k++) cout<<array[i][k];
          }
       cout<<endl;

    return 0;
}
// we have to save the data in an array so we have to work with addresses.
char myfunc ( ifstream &fin_ref , char &c)
{
    fin_ref>>c;
    return c;
}

NEEEEEEDDDDDD HELLLPPPPPP !!! :))

WaltP commented: There is ALWAYS a Space after each sentence in English. Please use them. It's required by the language. -3

If you know how to use linked lists you would be better off with a linked list of lines than that very huge array of strings. Something like this

struct node 
{
   struct node* next;
   char line[255];
};

Since this is c++ you could use std::vector instead of the above linked list, assuming you know how to use vectors and your teacher will allow it.

The next thing to do is NOT assume each line will contain exactly 1000 chracters. All you have to do is loop until '\n' is encountered.

int i = 0;
    char buffer[255] = {0};
    while( in.get(buffer[i]) && buffer[i] != '\n')
    {
       ++i; 
    }

The next thing to do is NOT assume each line will contain exactly 1000 chracters. All you have to do is loop until '\n' is encountered.

int i = 0;
    char buffer[255] = {0};
    while( in.get(buffer[i]) && buffer[i] != '\n')
    {
       ++i; 
    }

Or better yet, make your buffer a string array[5000]; and read one full line at a time with getline() rather than thousands of individual character reads.

Walt: He said he couldn't use std::string or getline().

Walt: He said he couldn't use std::string or getline().

Oh, sorry. I was blinded by the red

If you know how to use linked lists you would be better off with a linked list of lines than that very huge array of strings. Something like this

struct node 
{
   struct node* next;
   char line[255];
};

Since this is c++ you could use std::vector instead of the above linked list, assuming you know how to use vectors and your teacher will allow it.

The next thing to do is NOT assume each line will contain exactly 1000 chracters. All you have to do is loop until '\n' is encountered.

int i = 0;
    char buffer[255] = {0};
    while( in.get(buffer[i]) && buffer[i] != '\n')
    {
       ++i; 
    }

The linked list can solve the problem , but my point is how can i store the data in array is there any solution for reading from file by reference ?

use the example code snippet I posted here, but instead of putting the line just read into a linked list copy it into that array you posted.

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.