Is there a built in means of reading a vector<char> line by line?

I have a file I'm reading into a vector<char> buffer, reading through it for keywords, and adding new lines between old lines at those keywords. Currently, I'm doing the reading in what I believe is the least ideal way, by reading character per character, and checking after the new line character.

I could use strings to just make this much simpler. The only problem is I'm using a C library, and have to frequently convert my strings to C strings. So, going forward I'm trying to use C strings in the spots where most of the heavy lifting is done to make replacing the default String implementation less of a headache once all the functionality is in.

I know Is there a built in means of reading a vector<char> buffer line by line? Will it work if I need to resize the buffer after an insertion?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#define EDIT_FILE std::fstream::out | std::fstream::in | std::fstream::ate

bool readFileIntoBuffer(std::fstream &file, std::vector<char> &buffer)
{
    std::streamsize size = file.tellg();
    file.seekg(0, file.beg);
    bool read = false;
    buffer.resize((unsigned long)size);
    if(file.read(buffer.data(), size))
        read = true;
    return read;
}

void editHeader(std::string fileName, std::string propertyType)
{
    std::string pt = propertyType;
    std::string fn = fileName;
    std::string hfullPath = "PathToFile" + fn;
    std::fstream hfile(hfullPath, EDIT_FILE);

    std::vector<char> buffer;
    if(readFileIntoBuffer(hfile, buffer))
    {
       char temp[256]
       memset(temp, '\0', 256 * sizeof(temp[0]));
       int index = 0;
       for(int i = 0; i < buffer.size(); ++i)
       {
           temp[index++] = buffer[i];
           if(buffer[i] == '\n')
           {
               // compare for keywords
               // if keyword found insert s
                 // buffer.insert(buffer.begin() + i + 1, s.begin(), s.end())
               memset(temp, '\0, 256 * sizeof(temp[0]));
               index = 0;
           }
       }
    }

    // write char buffer to hfile from beginning and close.
} 
Last edited on
I could use strings to just make this much simpler. The only problem is I'm using a C library, and have to frequently convert my strings to C strings.

Why is that an issue?
closed account (E0p9LyTq)
The only problem is I'm using a C library, and have to frequently convert my strings to C strings.


Never heard of std:string's c_str() operator?
http://www.cplusplus.com/reference/string/string/c_str/
I'm going to make the code multithreaded, and from what I've read char * perform much better, since they get tossed on the stack. I also want to avoid frequent calls to c_str(). The full code is for a parser generating Unreal Game code that I would prefer to have scale well for larger data sets.

https://www.quora.com/Which-are-faster-in-C++-strings-or-char-array

I'm going to make the code multithreaded, and from what I've read char * perform much better, since they get tossed on the stack.


The issues are the same with multi-threading whether you manage the memory manually or a container (such as std::string) does it for you.

You should avoid advice on optimization when you don't know whether or not it matters. Yes, using a stack based memory scheme is more performant than using std::string.. but does it matter? And if it does, why not use a custom allocator instead?

I also want to avoid frequent calls to c_str().

Again, why? It costs nothing in terms of performance.
Last edited on
For me this is mostly a learning exercise. A custom allocator would definitely be ideal. I do plan on implementing that down the line. I'll most likely be making a stack allocator.

The big reason why I want to avoid the standard library is I'm interested in going into Gameplay or Game Engine programming. For various reasons in game programing the standard library is avoided, mostly historical reasons at this point. The biggest arguments I currently find valid are for reducing binary size, and compile times for large projects. I've been working a decent amount with Unreal, which doesn't support the standard library, but does use templates heavily.

I'd like to ween myself off of relying on templates mainly since using them slows compile times down significantly.
Topic archived. No new replies allowed.