Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#205279 - 17/02/2004 21:13 array of strings in STL C++?
image
old hand

Registered: 28/04/2002
Posts: 770
Loc: Los Angeles, CA
i want to read a text file into an array of strings. this isn't possible, is it? i'm going to have to use characters instead? anyone have a suggestion?

Top
#205280 - 18/02/2004 02:22 Re: array of strings in STL C++? [Re: image]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5682
Loc: London, UK
Hold on a second, I'll cobble something together.
_________________________
-- roger

Top
#205281 - 18/02/2004 02:36 Re: array of strings in STL C++? [Re: Roger]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5682
Loc: London, UK
/* read_lines.cpp

*/

#include <stdio.h>
#include <string>
#include <vector>

int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "Usage: read_lines filename\n");
return 1;
}

FILE *fp = fopen(argv[1], "r");
if (!fp)
{
fprintf(stderr, "Failed to open %s\n", argv[1]);
return 2;
}

typedef std::vector< std::string > lines_t;
lines_t lines;

const size_t buffer_size = 1024;
char *buffer = new char[buffer_size];

std::string residue;

while (!feof(fp) && !ferror(fp))
{
size_t bytes_read = fread(buffer, 1, buffer_size, fp);

const char *buffer_end = buffer + bytes_read;

const char *b = buffer;
const char *e;
while((e = (const char *)memchr(b, '\n', buffer_end - b)) != NULL)
{
std::string line(b, e);
residue += line;
lines.push_back(residue);
residue.clear();

b = e + 1;
}

// Is there any residue?
if (b < buffer_end)
{
// There's some residue
residue += std::string(b, buffer_end);
}
}

if (!residue.empty())
lines.push_back(residue);

delete [] buffer;

fclose(fp);

int n = 0;
for (lines_t::const_iterator i = lines.begin(); i != lines.end(); ++i)
{
++n;
printf("%d: %s\n", n, i->c_str());
}

return 0;
}
_________________________
-- roger

Top
#205282 - 18/02/2004 02:40 Re: array of strings in STL C++? [Re: Roger]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5682
Loc: London, UK
And a quick description of how it works. The main body of the program reads the named file into a vector of strings.

It does this by allocating a buffer and reading the file into it until it runs out of file. For each buffer of data that it reads from the file, it looks through for line-breaks, using memchr.

Because the buffer might not finish with a complete line, we collect the last part of the buffer into a string called 'residue'. So we need to remember to keep it around for the start of the next buffer.

When we run out of file, we might still have some residue, so we add this to the vector as well.

Then, when we're done, we print out the vector to the screen.
_________________________
-- roger

Top
#205283 - 18/02/2004 09:14 Re: array of strings in STL C++? [Re: Roger]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
Oh, my lord. Fifty-five lines just to read in a file line-by-line and print it back out! And people complain about C being too low-level.
_________________________
Bitt Faulk

Top
#205284 - 18/02/2004 09:17 Re: array of strings in STL C++? [Re: Roger]
image
old hand

Registered: 28/04/2002
Posts: 770
Loc: Los Angeles, CA
cool. funny how i could understand what needs to happen much easier using c functions, when i was trying to do it with fstream and ending up with really jumbled up code. thanks roger.

Top
#205285 - 18/02/2004 13:52 Re: array of strings in STL C++? [Re: image]
siberia37
old hand

Registered: 09/01/2002
Posts: 702
Loc: Tacoma,WA
Use delphi instead:

procedure ReadTextFile(AFile : String);
var
SL : TStringList;
begin
SL := TStringList.Create;
try
SL.LoadFromFile(AFile);
// strings are now in the stringlist object
finally
SL.Free;
end;
end;

Top
#205286 - 18/02/2004 14:01 Re: array of strings in STL C++? [Re: siberia37]
JBjorgen
carpal tunnel

Registered: 19/01/2002
Posts: 3583
Loc: Columbus, OH
C'mon... be fair to Roger...he did print them back out...

Or Perl:
#!/usr/bin/perl


open F,"test.pl";@l=<F>;close F;print @l;



_________________________
~ John

Top
#205287 - 18/02/2004 14:12 Re: array of strings in STL C++? [Re: JBjorgen]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
or Python:
f = file("file.txt", "r")

l = f.readlines()
f.close()
_________________________
Bitt Faulk

Top
#205288 - 18/02/2004 14:14 Re: array of strings in STL C++? [Re: JBjorgen]
julf
veteran

Registered: 01/10/2001
Posts: 1307
Loc: Amsterdam, The Netherlands
Or shell:

#/bin/sh

cat $MYFILE

#

Top
#205289 - 18/02/2004 14:20 Re: array of strings in STL C++? [Re: julf]
JBjorgen
carpal tunnel

Registered: 19/01/2002
Posts: 3583
Loc: Columbus, OH
julf - requirement was that it had to read the lines of the file into an array.
_________________________
~ John

Top
#205290 - 18/02/2004 15:21 Re: array of strings in STL C++? [Re: JBjorgen]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
Have to use ksh. No lesser sh has arrays.
#!/usr/bin/ksh

exec 5<file.txt
set -A array
i=0
while read -ru5 array[$i]; do
i=$(($i+1))
done

# And, to be complete
i=0
j=${#array[@]}
while [ $i -lt $j ]; do
echo ${array[$i]}
i=$(($i+1))
done
_________________________
Bitt Faulk

Top
#205291 - 19/02/2004 12:54 Re: array of strings in STL C++? [Re: JBjorgen]
julf
veteran

Registered: 01/10/2001
Posts: 1307
Loc: Amsterdam, The Netherlands
requirement was that it had to read the lines of the file into an array.

Duhh. Guess I should learn to read

Top