detecting objects and methods

hey guys,
i have a task which involves being given a source code and printing out the LOC in each object and each method along with total LOC in file and number of methods

i am racking my brain on how to detect an object or method

any ideas???

also i am having another problem
i open a text file using the ifstream, and read the lines from that file
these lines are the names of other files, once EOF is reached i close the open file
i then want to use ifstream again to open one of the files mentioned in the file that was just open, but this isnt seeming to work

here is some pseudocode to illustrate what i mean
ifstream = infile;
infile.open("file containing other file names")
// store filenames in linked list
if(EOF)
infile.close()

//go thru linked list one at a time
ifstream.open(linkedlist->data) <---- this line doesnt seem to work

ETC

thanks in advance
Hello, I just made this example.

The most important thing for opening a file is that the filename is of type

const char *

in the example you can see how you cast from string to char * and from char * to const char *.

I created a vector called linked_list in which all the filenames of the input file get stored.

after that I open all the files from this list one by one and put them in the infiles struct that I declared. In here as well the filenames as the retrieved strings from those files get stored.

finally I have a function that diplays everything thats loaded in memory.

also something I just found out is that when you open and close an fstream object, and after that you open it again, that it's needed to force the filepointer back to the begin of the file (or whatever point you want it) If you don't, file.eof() might return true for a file that has just been opened.


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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// list_of_files.cpp : main project file.
#include "stdafx.h"  // only for visual studio
#include <vector>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct infiles
{
    string filename;
    vector<string> filecontents;
};

vector<infiles> loaded_files;
vector<string> linked_list;

int main(int argc, char* argv[])
{
    void show_loaded_files();
    void load_linked_list();
    void load_linked_list_files();

    load_linked_list();
    load_linked_list_files();
    show_loaded_files();

    return 0;
}

void load_linked_list()
{
    string loadedfilename;
    fstream file;

    file.open("filenames.txt", ios::beg | ios::in );
    if(file.is_open())
    {
        while(!file.eof())
        {
            getline(file,loadedfilename);
            linked_list.push_back(loadedfilename);
        }
        file.close();
        }
        else
        {
            cout<<"cannot open filenames.txt\n";
        }
}

void load_linked_list_files()
{
    fstream file;
    char * cfilename;
    string currentline;

    for(int x=0;x<(int)linked_list.size();x++)
    {
        cfilename = new char[linked_list[x].size()+1];
        strcpy(cfilename,linked_list[x].c_str());

        file.open((const char*)cfilename, ios::in );
        file.seekg (0, ios::beg);

        delete cfilename;
        infiles *currentfile = new infiles;
		
        if(file.is_open())
        {
            currentfile->filename=linked_list[x];
            while(!file.eof())
            {
                getline(file,currentline);
                currentfile->filecontents.push_back(currentline);
            }
            file.close();
            loaded_files.push_back(*currentfile);
            delete currentfile;			
        }
        else
        {
            cout<<"cannot open: "<<linked_list[x]<<endl;
        }
    }
}

void show_loaded_files()
{
    for( int x=0 ;x<(int)loaded_files.size() ; x++)
    {
        cout<<loaded_files[x].filename<<" contains: "<<endl;
        for(int y=0;y<(int)loaded_files[x].filecontents.size();y++)
        {
            cout<<loaded_files[x].filecontents[y]<<endl;
        }
    }
}


I hope this is of any use.

Regard Ronnie van Aarle.
hey ronnie
thank you very much
i believe this line file.seekg (0, ios::beg);
is what wasn't working for me, because it was always detecting a EOF in every file apart from the first
appreciate it alot!!!
regards chris
Topic archived. No new replies allowed.