file opening/checking file

allright this is my pseudo code i was trying to write in c++. As most of you probably noticed im a c style coder but im learning c++ step by step

what im trying to write is a function that will open a file named init.conf and read the list of files that are located in the init.conf file and check if it exists and if it does to see its size


if you try this pseudo function i get the output "could not read files in init.conf". I've tried going to freenodes general c++ irc chatroom and they told me the code is horribly written and there are some details that are useless

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
    char config_init(char config_list[25][50]){
            int n = 0;
            long begin,end, totes;
            string file;
            char file_foo[50];
            ifstream config("init.conf");
            if(config.is_open()){
                    while(config.good()){
                            getline(config, file);
                            for(int i = 0; i > 50; i++){
                                    file[i] = config_list[n][i];
                                    file[i] = file_foo[i];
                            }
                    ifstream funcobj(file_foo);
                    if(funcobj.is_open()){
                            begin = funcobj.tellg();
                            funcobj.seekg(0, ios::end);
                            end = funcobj.tellg();
                            totes = end - begin;
                            cout << file << " is: " << totes << " bytes\n";
                            funcobj.close();
                            }
                    else{
                            cout << "could not read files in init.conf";
                            exit(0);
                            }
                    n++;
                    }
                    config.close();
            }
            else{
                    cout << "could not open init.conf";
                    exit(0);
                    }
            return config_list[25][50];
    }
bump
they told me the code is horribly written

They are right :P

You should use std::string instead of char arrays. Also, confing_list should probably
be a std::vector < std::string >. Another good idea is to break the task in smaller parts.
Write a function that checks whether a file exists or not. Then, write a function that returns
the size of an existing file. Then, use these two to implement your config_init function.

Useful links:

http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/vector/vector/
http://www.google.gr/search?q=c%2B%2B+file+size
I don't inderstand the purpose of these lines:
1
2
3
4
5
6
    getline(config, file);
    for (int i = 0; i > 50; i++)
    {
        file[i] = config_list[n][i];
        file[i] = file_foo[i];
    }

a line is read from the file config into the string file.
and then what... well I was going to say the string is immediately over-written with some other data.
But in fact, the condition i > 50; will be false, so the loop does not execute.

After that, this line, ifstream funcobj(file_foo); attempts to open the file named file_foo - which is just an uninitialised array of characters, containing any garbage data.

Why not do this:
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
void config_init(char config_list[25][50])
{
    int n = 0;
    long begin,end, totes;
    string file;

    ifstream config("init.conf");

    if (config.is_open())
    {
        while (getline(config, file))
        {
            ifstream funcobj(file.c_str());

            if (funcobj.is_open())
            {
                begin = funcobj.tellg();
                funcobj.seekg(0, ios::end);
                end = funcobj.tellg();
                totes = end - begin;
                cout << file << " is: " << totes << " bytes\n";
                funcobj.close();
            }
            else
            {
                cout << "could not read files in init.conf";
                exit(0);
            }
            strcpy(config_list[n], file.c_str());
            n++;
        }
        config.close();
    }
    else
    {
        cout << "could not open init.conf";
        exit(0);
    }

}

Last edited on
All that matters is that it works for you.

For someone else reading the file, some of your names are a bit odd.
for example:
string file;

when I see file, i expect that to be the file name, input or output file.
I would recommend
string line;

also I would change, just to make the names more meaningful, but that's a personal choice. Not necessary.
ifstream config("init.conf");

to
ifstream inputfile("init.conf");

---
To understand what your file is reading I would need to see the init.conf.

without your entire file i can't compile/test it. I don't see a reason why your file isn't opening but to debug should be simple enough.

Try these changes and see what you can learn from the output;
1
2
3
4
5
6
7
8
9
10
11
                    while(config.good())
					{
                            getline(config, file);
                            for(int i = 0; i > 50; i++)
							{
                                    file[i] = config_list[n][i];
                                    file[i] = file_foo[i];
                                    cout << file_foo[i] << endl;
                        	}
                    ifstream funcobj(file_foo);
                    cout << file_foo << endl;

Last edited on
I would certainly replace the parameter char config_list[25][50]
with an array (or even better, a vector) of std::strings.
Chervil, wow this thank you alot, is there a place where i can learn c++ intermediate
Topic archived. No new replies allowed.