Probs

This is a discussion on Probs within the C++ Programming forums, part of the General Programming Boards category; How do i load a whole file in to an array?...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    Probs

    How do i load a whole file in to an array?

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Probs
    I didn't even need to read the post, your thread subject told me EVERYTHING I needed to know about your problem.

    Read the rules and post properly next time.

    How do i load a whole file in to an array?
    Look up the ReadFile() function.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    How do you want your array set up? You could have every element of the array be a character, a line, or a paragraph. You could even have something else depending on how your file is formatted. You need to be a helluva lot more specific. Posting code would be very helpful.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    i'm assuming your data file is set up the way i have mine for the battle system... in that case...
    Code:
    	ifstream infile("stats.dat");	//open stats file
    	while(infile.getline(filename,20,' '))	//while more usernames, take it in
    	{
    		if(!strcmpi(filename,username))	//if filename matches
    		{
    			if(debug)
    			{
    				cout<<username<<"::"<<filename<<endl;	//test string
    				system("pause");	//test pause
    			}
    			infile.ignore(100,'h');	//ignore password field
       			infile>>hp>>null>>atk>>null>>def>>null>>lvl>>null>>exp>>null
       				>>mon>>null>>points;
       			break;	//end loop
       		}
       		else
       			infile.ignore(1000,'\n');	//ignore rest of line
    	}
    	infile.close();	//close file
    just modify that to fit your needs
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Here is some code I've used before to read in a file into an array...maybe you'll find it usefull somehow:
    Code:
    void readFromInputFile( char *text,     // large array to store input text
                            long &index,    // number of input file characters
                            char fileName[])// filename to use for input
    {
       ifstream inStream;                  // Input file stream
       ofstream outStream;                 // Output file stream
    
       // Set value of index to start at 0, in case we're calling this function a second time
       index = 0;
    
       // open input file
       inStream.open(fileName);   // associate the actual file name with "inStream"
       if ( inStream.fail() ) {
          cout << "Input file opening for " << fileName << " failed.\n"
               << "The file either doesn't exist or is not in the current directory.\n"
               << "Exiting...\n\n" << endl;
          exit(-1);
       }
    
       /*
       // open output file for debugging purposes
       outStream.open("output.txt");   // associate the actual file name with "inStream"
       if ( outStream.fail() ) {
          cout << "Output file opening for output.txt failed.\n"
               << "The file either doesn't exist or is not in the current directory.\n"
               << "Exiting...\n\n" << endl;
          exit(-1);
       }
       */
    
       // Read in the characters into our big array
       while ( inStream.get(text[ index++]) ) {
          if ( index >= MaxSize) {
             cout << "MaxSize of " << MaxSize << " was reached.\n";
             break;
          }
          // outStream << text[ index-1];     // for debugging
       }
    
       // close the input file
       inStream.close();
    
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2 simple C++ probs
    By alikoolg in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2009, 09:02 PM
  2. Replies: 2
    Last Post: 11-04-2007, 11:55 AM
  3. WM_TIMER probs (FPS Calculations)
    By C+noob in forum C++ Programming
    Replies: 0
    Last Post: 10-02-2005, 02:39 AM
  4. Several Probs
    By gamer4life687 in forum C++ Programming
    Replies: 4
    Last Post: 12-25-2002, 06:40 PM
  5. magic sq probs
    By scuba22 in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2002, 08:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21