Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ File Input to Vector

Name: foreach(nigger in Africa) 2011-12-15 1:12

Well /prog/ I'm trying to read a text file line-by-line into a vector. Is there some sort of inherent vector size limitation I don't know about?

Protip: The file has 23,000 lines. :S

(I've tested it by trying to output the first 1 or 2 vector elements after the loop, but shit just breaks because there's nothing in it??)

Name: Anonymous 2011-12-15 1:22

C++ is shit and I have never used it. But certainly it should be able to handle a vector of strings? Just make a vector of strings and read them one at a time as strings, then append them to your vector.

Name: Anonymous 2011-12-15 1:24

Name: Anonymous 2011-12-15 1:44

5 people liked this.

Name: Anonymous 2011-12-15 1:45

23,000 lines.
TF? I thought /prog/ told me that SEPPLES was most powful and perfect, I cons up millions of lines from files in shit Lisps to make the GC shit its pantsu, IHBT.

Name: Anonymous 2011-12-15 6:20

IOstream == shit

Name: Cudder !MhMRSATORI!FBeUS42x4uM+kgp 2011-12-15 7:19

Is there some sort of inherent vector size limitation I don't know about?
Yes, it's called the process memory limit.

Name: Anonymous 2011-12-15 14:37

>>7
Usually 2GB on a 32-bit system.  No idea what it is in Win64 because I am old school.

I am surprised that 23000 lines is enough to hit the limit, whatever it is, unless the lines are an average of 100000 characters long.

Name: Anonymous 2011-12-15 15:22

>>8
OP is prolly doing it wrong, the memory limit is purely theoretical, since op did not post codan, this must be a trollpost, labeling OP a faggot.

Name: Anonymous 2011-12-15 15:32

>>9

    string line;
    vector<string> lines;

    ifstream myFile; //Set vector elements to lines from text file
    myFile.open("ex100926.txt"); //Open txt file
    while (getline(myFile, line))
    {
        lines.push_back(line);
    }
    //Close txt file
    myFile.close(); //Close txt file

    cout<<lines.at(0)<<endl;


This gives me a vector error, because apparently the lines from the text file aren't being read or put into the vector?

I basically have a 100% identical code for another program, which places each string into an array rather than a vector, and that one works 100% fine. (and yes, the file is there and has text in it)

Name: Anonymous 2011-12-15 15:41

if you were going out of range this shit would be thro
wn at you
std::out_of_range

Name: Anonymous 2011-12-15 15:48

>>10
that code is actually correct and working, fuck up elsewhere OP

Name: Anonymous 2011-12-15 16:00

>>12

That's the sad part: this is basically ALL the code I have so far. I DON'T EVEN-


#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    string line;
    vector<string> lines;

    ifstream myFile; //Set vector elements to lines from text file
    myFile.open("ex100926.txt"); //Open txt file
    while (getline(myFile, line))
    {
        lines.push_back(line);
    }
    //Close txt file
    myFile.close(); //Close txt file

    cout<<lines.at(0)<<endl;

    system("pause");
    return 0;
}


It has to be a problem with the text file, what else could it be?

Name: Anonymous 2011-12-15 16:06

"stdafx.h"
Visual bullshit detected, did you place your textfile in the right place /debug folder or w/e,
also some IDEs (fuck you netbeans!) need the file in the project folder when ran through the debugger,
check the is_open()

Name: Anonymous 2011-12-15 16:54

>>14

That's just some generic visual studio bullshit.

The text file is in the same location as the text file in my other program, which runs just fine.

Name: Anonymous 2011-12-15 17:57

Well what do you want, OP, for us to teach you how to debug?  The one good thing that can be said about Visual Studio is that it has excellent debugging features.  Set some breakpoints, catch some exceptions, figure out what the fuck is going wrong.

Name: Anonymous 2011-12-15 18:12

>>16

Except therein lies the problem. As far as the debugger or anyone is concerned, this program SHOULD work.

Name: Anonymous 2011-12-15 18:16

>>17
Well, then I guess you just have to give up.  The program should work but doesn't, so there is a bug in the universe that is preventing it from working.  You might get into Wired Magazine for discovering this.

Name: Anonymous 2011-12-15 19:05

How long did you wait for something to such up since it's Microsoft Visual Retard (prolly auto'd to Managed C++ too)
it might just be fuck slow to parse 23k lines, did you see the Pause message show up?
(srsly my cygwined gcc is faster on windows than the native Microsoft stuff, go MS!)

Name: Anonymous 2011-12-15 19:08

>>19
Thank you for at least not using a dollar sign for an ``S''.

Name: Anonymous 2011-12-15 19:46

>>20
M$ is $hit, and so is Window$.

LONG LIVE GNU

Name: Anonymous 2011-12-15 21:07

getline returns an istream &
why doesn't this code loop forever?

Name: Anonymous 2011-12-15 21:14

>>19
The pause message shows up almost instantaneously. (Text file line parsing is extremely fast regardless, in comparison to something like a URL download to file)

>>22
Because it only loops until there aren't any more lines to read from the text file

Name: Anonymous 2011-12-15 21:35

>>23
No, no, no. The return type is an istream reference, specifically a reference to the istream that the input was taken from. This reference should always be valid. Therefore, the loop should loop forever.... Maybe it's converted to a void *?...

Anyway. I chopped off the first line (MS specific stuff) and compiled with the text file set as the source file and it worked fine under Cygwin.

Name: Anonymous 2011-12-16 0:27

>>24

I don't know, I basically have the same exact method of file input on another program that works 100% fine with essentially the exact same code..

Name: Anonymous 2011-12-16 1:05

>>25
That's really interesting!

Name: Anonymous 2011-12-16 6:34

>>24
This reference should always be valid. Therefore, the loop should loop forever.... Maybe it's converted to a void *?
Yes. ifstream defines a ` void * operator` which returns a non-null if the file is readable or null if the file is not open or there are no more data or an error occurred.

>>17
Except therein lies the problem. As far as the debugger or anyone is concerned, this program SHOULD work.
Are you retarded? Do you understand what a "debugger" is? You don't ask the debugger what it is concerned about, you run a program in the debugger and see why it doesn't work.

Or you can add `printf("%d\n", myFile.is_open());` and see that you failed to put the file in the same directory as your program, you fucking retarded moron.

Name: Anonymous 2011-12-16 6:49

$ g++ -xc++ -
#include <ifstream>
main() {
std::ofstream f("find_me");
x << "gotcha!";
}

$ ls
.
..
find_me

Name: Anonymous 2011-12-16 9:47

>>27
debugger you so tsundere, you never tell me what are you concerned about,
BTW: I compiled this bull in M$ Visual Niggerudio, made a 180k lines of 'fagstorm' 27 times per line, and it fucking worked.
Also if the vector read nothing the .at(0) at end should throw shit at him.
So wtf?
OP post 1st line of the file

Name: Anonymous 2011-12-16 14:37

>>29

First 3 lines:

#Software: Microsoft Internet Information Services 6.0
#Version: 1.0
#Date: 2010-09-27 00:00:00

Name: Anonymous 2011-12-16 18:36

23000 lines
Assuming 80 characters per line average

23000 * 80 = 1840000 bytes
Adding on a few extra bytes for the other shit in the vector...

Your computer has more than 2 Megabytes of RAM, right OP? Because if it has more than 2 Megabytes of RAM, you should be fine making a vector of 23000 strings. +

Name: Anonymous 2011-12-16 18:41

>>31

Welp, idk wtf is up then...

Name: Expert Jew 2011-12-16 20:11

I lost members of my family in auschwitz I don't find that funny at all would you still find it funny if members of your family had been murdered in a death camp by the Nazis I can't see there is nothing remotely about 6.000.000 people being systematically and deliberately murdered

Name: Anonymous 2011-12-16 20:13

>>33

That is HILARIOUS! Holy shit, you should do standup.

Don't change these.
Name: Email:
Entire Thread Thread List