Re: reading from a file into an array

From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 11/07/03


Date: Fri, 07 Nov 2003 20:49:40 +0100

Allen Seelye wrote:

> Newbie here. I'm having a hell of a time with this. It takes input
> from the console into an array and then dumps it into a file. that all
> is pretty straight forward and works just fine. The output file looks
> like this:
>
> 1,John Smith, 5551234, 1515 Some St
> 2,Jane Smith, 5555678, 1515 Someother St
>
> ...and so on. I've had no success getting this read back into an
> array. I've got three books on C++ and none of them has any
> information on how to accomplish this.

Then throw them all away. This is pretty basic stuff.

> Even if someone could show me how to read each line in as a single
> string I could probably figure it out from there on my own.

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

int main()
{
    std::ifstream file("thefile.txt");
    std::string s;

    while (std::getline(file, s))
        std::cout << "Got a line: " << s << '\n';

    if (!file.eof())
        std::cout << "Error reading file\n";
}



Relevant Pages

  • reading from a file into an array
    ... the console into an array and then dumps it into a file. ... 2,Jane Smith, 5555678, 1515 Someother St ... I've had no success getting this read back into an array. ...
    (comp.lang.cpp)
  • Re: save array to file, read it back in
    ... The ability to read the contents of binary files and dumps with "stuff" in them, although increasingly becoming a lost art, is still important in applications such as those employing serial comms. ... void SaveArray(CString Filename,unsigned char *pBuffer,long Size) ... //first write out the array size ...
    (microsoft.public.vc.mfc)
  • Re: inspecting the current definition of a method
    ... method's opcodes into a big array of hashes, ... but it seems that this is less straight forward than I have hoped ...
    (comp.lang.ruby)
  • Re: .NET 2.0 DateTime conversion error??
    ... I pasted the code you gave me into a console application, ... however what array is it talking about? ... Tim ... > Otis Mukinfus ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: array formula Excel 97 (Poker)
    ... Since the array trigger is a constant array, ... Ace high straights should always be allowed. ... but J/Q/K/A/2 has never been a straight in any hand I've ...
    (microsoft.public.excel.misc)