Jump to content

How to read text file into array in C++?

- - - - -

  • Please log in to reply
8 replies to this topic

#1
bexita

bexita

    Newbie

  • Members
  • PipPip
  • 15 posts
Hi,
I have this text file, the format looks like this:

2      (no of modules)

Module123   (Module code)

Module100    


5

Module123   (Module code)

Module111

Module245

Module232

Module343

I want to read this into array of structure :
struct student {
int no_module;
--> how to declare variables for the module ?? <----
};
student a[];
file.open("data.txt" , ios ::in);

-->Because its based on the number of modules, if they are 2 , there are two modules which will be read or if they are 5 , there are 5 modules which will be read only. Anyone know how to read this text files ?
Thanks a lot.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It looks like you would use an array of strings. Are you doing this in C or C++?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
bexita

bexita

    Newbie

  • Members
  • PipPip
  • 15 posts
Im using C++ ,....

Can u explain more?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Maybe I should back up. Since you haven't provided much code, I'm guessing you aren't worried about reading the file, just how to store it. The two methods for storing a large amount of data in sequence would be an array or std::vector. Given that the data you're storing appears to be strings ("Module123", "Module100", etc), it seems logical that you would store an array or vector of strings.

1) is that all correct?
2) does it make sense? If not, what part doesn't?

If you don't know what an array of strings is, then we have a much bigger problem. If I'm incorrect about the structure of the data to be stored, perhaps a clearer example is in order.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
denarced

denarced

    Programmer

  • Members
  • PipPipPipPip
  • 182 posts
I'd say you need something like:

/** Read file line by line until line consists of a number.
 */
int readUntilCount(std::ifstream& files);

int main(int argc, char** argv)
{
    std::ifstream textFile("data.txt");
    if(textFile.is_open())
    {
        while(textFile.good())
        {
            const int moduleCount = readUntilCount(textFile);
            if(moduleCount < 1)
                break;
            for(int i=moduleCount; i > 0; --i)
            {
                ; //    
            }
        }
    }
    return 0;
}




#6
bexita

bexita

    Newbie

  • Members
  • PipPip
  • 15 posts
Hi , my code looks like this but it displayed wrongly:
while (afile >> s[i].k_value)

	{

		

			

			afile >> s[i].name;

			afile >>s[i].no_module;


			int no= s[i].no_module;

			for (;i<no; i++)

			{

				afile.getline(s[i].m_code, MAX,'\n');

				

			}

		

		

	i++;

			

	}	 

whats wrong with this code of store?

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What are your variables?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
bexita

bexita

    Newbie

  • Members
  • PipPip
  • 15 posts

struct student{

int k_value;

char name[200];

int no_module;

char module_code[200]

};


//Storing info in the txt file

while (afile >> s[i].k_value)  // a random student no stored in struct

	{		

			afile.getline (s[i].name,200,'\n');

			afile >>s[i].no_module;// till here its ok for output text file.


			int no= s[i].no_module; 

			for (int j=0;j<no; j++) 

			{

				afile >> s[i].module_code[j]; // it displayed wrong in the output file

				

			}

		

		

	i++;

			

	}

I still stuck at storing the info of this text file. Pls advise.
THanks

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Here's your biggest problem. You are not posting code that anyone else can compile. s is not defined. afile is not defined. What is your code?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users