Read a .txt file into dynamic CString array

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 5656 Views - Last Post: 26 February 2011 - 10:56 PM Rate Topic: -----

#1 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Read a .txt file into dynamic CString array

Posted 26 February 2011 - 12:17 AM

Hello everyone,
I am trying through a text file of processes, and I want it to store in a CString array.
I was trying to make the array dynamic which is kind of giving me some trouble.
I am using CString because of its capability to read each character.
I have pasted my code here, please some one help me out in dynamically allocating space in array.
I have been able to read from the file and calculate the # of lines stored in variable numOfProcesses but cannot take it any further.

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


/**struct Processes
{
       string Block_Size;
       string PID;
       string CPU;
       string IN;
       string OUT;
       string READ;
       string WRITE;
       string SIGNAL;
       string WAIT;
};**/

int numOfProcesses = 0;

int ReadFile(ifstream&); //Reads file and counts number of lines
void EnterProcess(ifstream&, char [], int&);//Read file and store each line in the array, dynamically allocate memory
void DispProcess(Processes*, int); //Display the array
 

int main()
{
    int *numPro = &numOfProcesses;
    
   // int numOfProcesses = 0; //collects number of lines from file
    
    ifstream fin;
    fin.open("Processes.txt");
    
    if(!fin.good())
    {
                   cout<<"Error opening file."<<endl;
                   fin.clear();
                   return -1;
    }
    
    numOfProcesses = ReadFile(fin);
    
    cout<<"numOfProcesses:"<<numOfProcesses<<endl;
    cout<<"numPro;"<<*numPro<<endl;
    
    char ProcessArray[numOfProcesses];
    
    fin.clear();
    
    fin.seekg(ios::beg); //goes back @ the start of file
    
    //Processes Osprocess[numOfProcesses];
   //Processes* Osprocess = new Processes[numOfProcesses];

    
   // DispProcess(Osprocess, numOfProcesses);
    
    
    
    
    
    
 system("Pause");
    return 0;
}

int ReadFile(ifstream& fin)
{
    string temp;
    int numOfProcesses = 0;
    
    while(getline(fin, temp))
    {
                       numOfProcesses++;
                       
    }
   
   return numOfProcesses;
}
    //Function to read and store file into Cstring  dynamic array
    void EnterProcess(ifstream& fin, char procArray[], int& numOfProcesses )
    {
         
         string temp;
         
         while(getline(fin,temp))    
         {
                                     for(int i = 0; i<numOfProcesses; i++)
                                     {
                                            // procArray[i]=temp;
                                     }
         }                                                                                                   
         
        while(fin 
         
                //   Oproces[numOfProcesses] = fin;
                  // numOfProcesses++;
         
         
                 >>Osprocess->Block_Size
                  >>Osprocess->PID
                  >>Osprocess->CPU
                  >>Osprocess->IN
                  >>Osprocess->OUT
                  >>Osprocess->READ
                  >>Osprocess->WRITE
                  >>Osprocess->SIGNAL
                  >>Osprocess->WAIT
               )
               {Osprocess++ ;}
               
         return;
    }
    
    void DispProcess(Processes*Osprocess, int numOfProcesses)
    {
         
         cout.setf(ios_base::left,ios_base::adjustfield);
         cout.setf(ios_base::showpos);
         cout.setf(ios_base::showpoint);
         cout<<"Block_Size";
         cout<<"\t PID";
         cout<<"\t CPU";
         cout<<"\t IN";
         cout<<"\t OUT";
         cout<<"\t READ";
         cout<<"\t WRITE";
         cout<<"\t SIGNAL";
         cout<<"\t WAIT"<<endl;
         
         for(int i = 0; i < numOfProcesses; i++)
         {
                 cout<<Osprocess[i].Block_Size;
                 cout<<Osprocess[i].PID;
                 cout<<Osprocess[i].CPU;
                 cout<<Osprocess[i].IN;
                 cout<<Osprocess[i].OUT;
                 cout<<Osprocess[i].READ;
                 cout<<Osprocess[i].WRITE;
                 cout<<Osprocess[i].SIGNAL;
                 cout<<Osprocess[i].WAIT;
         }
         
         return;
    }
    


I would appreciate if someone can explain it to me rather than paste a link .
Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Read a .txt file into dynamic CString array

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 12:25 AM

You have declared an array of characters but I also see a structure you made for a process. So I guess to store all processes you need to make an array of Processes
Processes ProcessArray[numOfProcesses];


Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 01:01 AM

An array of cstrings will be horribly messy to deal with when you have many processes, each one involving 9 strings.

And your reason for preferring cstrings over c++ strings doesn't seem to make sense -- you can access individual characters in c++ strings. But anyway, even if you choose to use cstrings, you should use a struct of 9 cstrings for each process.

Either way, after you have counted the number of processes, allocate memory for the array like so (assuming you are using a struct Processes consisting of either strings or cstrings):

    Processes* processArray = new Processes[numOfProcesses]



Then you can access the individual strings or cstrings as processArray[0].Block_Size, processArray[0].PID, etc.


Don't forget to delete[] processArray before the end of your program.

This post has been edited by r.stiltskin: 26 February 2011 - 01:07 AM

Was This Post Helpful? 1
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 03:02 AM

Just use C++ strings and all your problems with dynamic sizing issues will immediately go away.

This is the same advice as r.stiltskin gave but they did it politely and nicely and I thought it worth stating the harsh facts straight out that your fundamental decision to use Cstrings is completely wrong (especially since you don't know how to write the code to make Cstrings work).

Change your fundamental decision. Use C++ strings. Feel the problems fall immediately from your shoulders.
Was This Post Helpful? 0
  • +
  • -

#5 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 02:05 PM

Thanks guys for the advice. I have made recommended changes & I am going with strings rather that cstrings.

But I have another question. I am trying to read from the file & store it to struct array but I am getting an error. Please can some one tell me what am I doing wrong.

Here is the function that reads & store the data into struct array

 void EnterProcess(ifstream& fin, Processes* processArray)
    {
         
         string temp;
         int i = 0;
         
         while(i<numOfProcesses && getline(fin,processArray[i]))
         {
                                fin>>processArray[i];
                                fin.ignore();
                                ++i;
         }
         
        /** while(getline(fin,temp))    
         {
                                     for(int i = 0; i<numOfProcesses; i++)
                                     {
                                            // procArray[i]=temp;
                                     }
         }                                                                                                   
         
        while(fin 
         
                //   Oproces[numOfProcesses] = fin;
                  // numOfProcesses++;
         
         
                 >>Osprocess->Block_Size
                  >>Osprocess->PID
                  >>Osprocess->CPU
                  >>Osprocess->IN
                  >>Osprocess->OUT
                  >>Osprocess->READ
                  >>Osprocess->WRITE
                  >>Osprocess->SIGNAL
                  >>Osprocess->WAIT
               )
               {Osprocess++ ;}**/
               
         return;
    }
 


Error: main.cpp In function `void EnterProcess(std::ifstream&, Processes*)':
no matching function for call to `getline(std::basic_ifstream<char, std::char_traits<char> >&, Processes&)'
Was This Post Helpful? 0
  • +
  • -

#6 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 02:32 PM

try #include <string> instead of #include <string.h>

Also getline() needs a reference to a string as second parameter and you are passing a structure (processArray[i]).

One more thing..in the following code :


while(i<numOfProcesses && getline(fin,processArray[i]))
   fin>>processArray[i];
   fin.ignore();
   ++i;
}


the function getline() reads a line and then you read a word using >>.
I guess you want to just read all the lines.

This post has been edited by chinchang: 26 February 2011 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

#7 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 02:36 PM

Hey everyone,
I have a text file with processes listed as such:

Block size 4096
New 10
cpu 10
...

I am trying to read the file into a struct array.

I have declard my stuct called Processes.

Have also declared an array of Processes.

The variable 'numOfProcesses is used to read & see #of lines in the file & allocate memory in array accordingly.

But when I try to read the file into the array using while loop, I get an error.
Here is the function where I am trying to read into array and I get an error:

 void EnterProcess(ifstream& fin, Processes* processArray)
    {
         
         string temp;
         int i = 0;
         
         
         
         while(i<numOfProcesses && (getline(fin,processArray[i])))
         {
                                fin>>processArray[i];
                                fin.ignore();
                                ++i;
         }

return;
    }




Error: main.cpp In function `void EnterProcess(std::ifstream&, Processes*)':
94: no matching function for call to `getline(std::basic_ifstream<char, std::char_traits<char> >&, Processes&)'


here is the whole program:


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


struct Processes
{
       string Block_Size;
       string PID;
       string CPU;
       string IN;
       string OUT;
       string READ;
       string WRITE;
       string SIGNAL;
       string WAIT;
};

int numOfProcesses = 0;

int ReadFile(ifstream&); //Reads file and counts number of lines
void EnterProcess(ifstream&, Processes*);//Read file and store each line in the array, dynamically allocate memory
void DispProcess(Processes*, int); //Display the array
 

int main()
{
    int *numPro = &numOfProcesses;
    
   // int numOfProcesses = 0; //collects number of lines from file
    
    ifstream fin;
    fin.open("Processes.txt");
    
    if(!fin.good())
    {
                   cout<<"Error opening file."<<endl;
                   fin.clear();
                   return -1;
    }
    
    numOfProcesses = ReadFile(fin);
    
    Processes* processArray = new Processes[numOfProcesses];
    
    //cout<<"numOfProcesses:"<<numOfProcesses<<endl;
    //cout<<"numPro;"<<*numPro<<endl;
    
   // char ProcessArray[numOfProcesses];
    
    fin.clear();
    
    fin.seekg(ios::beg); //goes back @ the start of file
    
    //Processes Osprocess[numOfProcesses];
   //Processes* Osprocess = new Processes[numOfProcesses];

    EnterProcess(fin, processArray);
   // DispProcess(Osprocess, numOfProcesses);
    
    
    
    
    
    
 system("Pause");
    return 0;
}

int ReadFile(ifstream& fin)
{
    string temp;
    int numOfProcesses = 0;
    
    while(getline(fin, temp))
    {
                       numOfProcesses++;
                       
    }
   
   return numOfProcesses;
}
    //Function to read and store file into Cstring  dynamic array
    void EnterProcess(ifstream& fin, Processes* processArray)
    {
         
         string temp;
         int i = 0;
         
         
         
         while(i<numOfProcesses && (getline(fin,processArray[i])))
         {
                                fin>>processArray[i];
                                fin.ignore();
                                ++i;
         }
         
        /** while(getline(fin,temp))    
         {
                                     for(int i = 0; i<numOfProcesses; i++)
                                     {
                                            // procArray[i]=temp;
                                     }
         }                                                                                                   
         
        while(fin 
         
                //   Oproces[numOfProcesses] = fin;
                  // numOfProcesses++;
         
         
                 >>Osprocess->Block_Size
                  >>Osprocess->PID
                  >>Osprocess->CPU
                  >>Osprocess->IN
                  >>Osprocess->OUT
                  >>Osprocess->READ
                  >>Osprocess->WRITE
                  >>Osprocess->SIGNAL
                  >>Osprocess->WAIT
               )
               {Osprocess++ ;}**/
               
         return;
    }
    
    void DispProcess(Processes*Osprocess, int numOfProcesses)
    {
         
         cout.setf(ios_base::left,ios_base::adjustfield);
         cout.setf(ios_base::showpos);
         cout.setf(ios_base::showpoint);
         cout<<"Block_Size";
         cout<<"\t PID";
         cout<<"\t CPU";
         cout<<"\t IN";
         cout<<"\t OUT";
         cout<<"\t READ";
         cout<<"\t WRITE";
         cout<<"\t SIGNAL";
         cout<<"\t WAIT"<<endl;
         
         for(int i = 0; i < numOfProcesses; i++)
         {
                 cout<<Osprocess[i].Block_Size;
                 cout<<Osprocess[i].PID;
                 cout<<Osprocess[i].CPU;
                 cout<<Osprocess[i].IN;
                 cout<<Osprocess[i].OUT;
                 cout<<Osprocess[i].READ;
                 cout<<Osprocess[i].WRITE;
                 cout<<Osprocess[i].SIGNAL;
                 cout<<Osprocess[i].WAIT;
         }
         
         return;
    }


This post has been edited by jingoria: 26 February 2011 - 03:29 PM

Was This Post Helpful? 0
  • +
  • -

#8 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 02:50 PM

just use a local string variable to read in the value, then store it in the struct array. Post your entire program as well, it's hard to tell from just what you posted.

This post has been edited by vividexstance: 26 February 2011 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#9 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 03:33 PM

View Postvividexstance, on 26 February 2011 - 02:50 PM, said:

just use a local string variable to read in the value, then store it in the struct array. Post your entire program as well, it's hard to tell from just what you posted.


I tried doing that like this:


string temp;

 while(getline(fin,temp))
         {
                               processArray[] = temp;
                                ++i;
         }



but that is not working either.
Was This Post Helpful? 0
  • +
  • -

#10 akhena  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 89
  • Joined: 20-January 11

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 03:41 PM

getline function doesn't work that way. istream& getline ( istream& is, string& str );. And you're passing a pointer of Processes as a second parameter.
So you should write something like :
getline(fin, temp);
ProcessArray[i]->set(temp); 

And you define the member function set in your struct, to make it assign the different parts of that string to the struct string data members.

This post has been edited by akhena: 26 February 2011 - 03:43 PM

Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 04:03 PM

The problem is on line 7: processArray[i] is a Processes structure, not a string. How to correct this depends on how your input file is set up. For each process, your file contains strings for
Block_Size, PID, CPU, IN, OUT, etc.
The question is, is each of those on a separate line in the file, or does a single line contain all of the entries for a particular process?
Was This Post Helpful? 0
  • +
  • -

#12 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 04:21 PM

You are creating a mess by having 2 threads about the same question.

Now here I see your description of the input file, so it is clear why your EnterProcesses function doesn't work. But explain with an example EXACTLY what you want to store in the various strings in the Processes structure.

If the input file says (based on your post #7 above):
Block size 4096
New 10
cpu 10
...

do you want to store "Block size 4096", or do you just want to store "4096" in the BLOCK_SIZE field?

(and if you only want 4096, is there a reason why you don't want to store that as an int?)

and so on...

You need to be clear about exactly what you are trying to achieve before you can get useful help.

This post has been edited by r.stiltskin: 26 February 2011 - 05:04 PM

Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,657
  • Joined: 23-August 08

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 04:59 PM

Merged duplicate topics.

jingoria, you've been here long enough to know better!
Was This Post Helpful? 0
  • +
  • -

#14 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 05:14 PM

View PostJackOfAllTrades, on 26 February 2011 - 04:59 PM, said:

Merged duplicate topics.

jingoria, you've been here long enough to know better!


Ok. Sorry about all that mess & not being specific enough. What I am trying to do is: I have a file with OS processes listed something like this:

Block size 4096
New 10
Cpu 20
..
New 15
cpu 18
.. and so on

(Each process is in a new line.)
Each of those 'New' indicates a new process. I am suppose to allocate a new array for each new process.
So my idea is to read the whole file in an array first (read everything not just integers) and then search through the array and everything that occurs from first 'New' is caught until the other 'New' is caught gets stored in another array.

This post has been edited by jingoria: 26 February 2011 - 05:18 PM

Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Read a .txt file into dynamic CString array

Posted 26 February 2011 - 05:23 PM

View Postjingoria, on 27 February 2011 - 10:14 AM, said:

So my idea is to read the whole file in an array first (read everything not just integers) and then search through the array and everything that occurs from first 'New' is caught until the other 'New' is caught gets stored in another array.


Okay, show us the code you have right now that tries to do that.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2