Hi,

Following is the function which is giving me a segmentation fault.
I do not know what is wrong in this code. I am counting the number of lines in the file.

char f11;
        char c;
        int f12 = 0;
        FILE *ilfile;

        ifstream ficnf;
        int size_f = 0;
        int num_p = 0;
        /* Count the number of lines in the file */
        string linef;
        ficnf.open(ftp,ios::in);
        if (!ficnf.is_open())
        {
                cout<< "Error opening file" << "\n";
                exit(-1);
        }
        else
        {
                while(getline(ficnf,linef))
                {
                        if(linef[0] == 'p' || linef[0] == 'c')
                        num_p++;
                        size_f++;
                }
                        printf("File contains lines = %d \n",size_f);
        }
        ficnf.clear();
        ficnf.close();
I am getting the follwoing trace when i use gdb debugger

#0  0x656c6c69 in ?? ()
#1  0xb75a2b68 in std::string::_Rep::_S_create(unsigned, std::allocator<char> const&) () from /usr/lib/libstdc++.so.5
#2  0xb759f38d in std::string::_M_mutate(unsigned, unsigned, unsigned) () from /usr/lib/libstdc++.so.5
#3  0xb75a36e3 in std::string& std::string::_M_replace_safe<char const*>(__gnu_cxx::__normal_iterator<char*, std::string>, __gnu_cxx::__normal_iterator<char*, std::string>, char const*, char const*) () from /usr/lib/libstdc++.so.5
#4  0xb75a04ba in std::string::replace(unsigned, unsigned, char const*, unsigned) () from /usr/lib/libstdc++.so.5
#5  0xb75929bd in std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, char) () from /usr/lib/libstdc++.so.5
#6  0xb7592ab8 in std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) () from /usr/lib/libstdc++.so.5
#7  0x0804b80b in circuit::correct_cnf_file(int, int, char*) (this=0x8078e68, no1=808, no2=2626, 
    ftp=0xbffff5d0 "../../sequential_miter/s382.cnf") at main_program.cpp:453
#8  0x0804bff5 in circuit::main_induction(char*) (this=0x8078e68, file_cnf=0xbffff770 "../../sequential_miter/s382")
    at main_program.cpp:580
#9  0x0804af2a in circuit (this=0x8078e68, cktName=0xbffff770 "../../sequential_miter/s382", 
    testvector=0xbffff740 "../../sequential_miter/s382") at main_program.cpp:298
#10 0x0804d54c in main (argc=3, argv=0xbffff834) at main_program.cpp:1116

Any help is appreciated.
Thanks

What happens when getline(ficnf,linef) fails ?

hi,

i am not sure i understand ur question. The file has 2000 lines and getline gives a segmentation fault after reading line number 891

Thanks

And how should we know what line 891 is?

commented: main_program.cpp:1116 - the prog is way over 1000 lines, so you're looking at a pin-hole of the real problem +29
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.