简体   繁体   中英

C++ : Error using argv[1] into a function to read a .txt file

I'm currently beginning on C++ and trying to make a function that can open a .txt file, read it, and save its words in an array (each word being a protein sequence). Unfortunately I don't succeed at calling the argument containing the name of the file ( argv[1] ) in the function. Can anyone spot errors in my code or in the way I implemented this ? Thanks in advance, you will find the code and the error messages below :

Libraries

So here are my librairies and 'shortcuts'.

// Librairies
#include <iostream>
#include <string>
#include <fstream>

// Alias
using std::cout;
using std::endl;
using std::string;

The function

Now this is the function, note that filename is supposed to be a string containing argv[1] (the name of a .txt file specified at execution) :

string SequenceChoice(int n, string filename); // Function returning a string

    std::ifstream sequenceFile (filename);   //Opens the file specified on execution

    if ( sequenceFile.is_open() )
    {
        cout<<"File opened"<<endl;

        string tmp;
        int i = 0;

        while( sequenceFile >> tmp )    // Counts the number of sequences (words)
        {
            i++; 
        }

        string allchains[i];   //  Creates an array of strings, to save all the words

        sequenceFile.clear();                   
        sequenceFile.seekg(0, sequenceFile.beg);  // Replaces the cursor at the beginning of the file
        i=0;

        while( sequenceFile >> allchains[i]) // Saves each word as a string in an array
        {
            cout << allchains[i] << tmp;    
            i++;
        }   

        sequenceFile.close();
        cout<< "File closed"<<endl;
    }

    else
    {
        cout << "Error: Cannot open file" << endl;
    }

    return allchains[n];            // returns the 'n'th word (n being specified when calling the function


// end of the function

Main

Now the main function, I'm not sure if doing string filename = argv[1] works, but I get less errors when I keep this step instead of putting argv[1] as an argument of my SequenceChoice() function.

int main(int argc, char *argv[]) {

    if(argc >= 2) 
    {
        string filename = argv[1];
        cout << SequenceChoice( 2, filename ) << endl; // I expect it to give me the 3rd word of a .txt file for example.
    }
    else
    {
        cout << "Error : No file" << endl; 
    }


    return 0;
}

The error message I get

Error message

The link above is a picture of the error message I get when I compile, I've been searching for hours on the internet how to resolve this, unfortunately I could'nt manage to have the code working. There's probably an error of type with the way I deal with argv[], but I failed at solving it so any help and comments would be greatly appreciated.

Please try changing following line

string SequenceChoice(int n, string filename); // Function returning a string

to

string SequenceChoice(int n, string filename) { // Function returning a string

You are trying to write a function but the ; is terminating your function and body is not starting. It should work. Also please read a book carefully.

This page lists very good books for all experience levels.

Try this :

string SequenceChoice(int n, string filename)
{ 
    std::ifstream sequenceFile (filename);   //Opens the file specified on execution

    if ( sequenceFile.is_open() )
    {
        cout<<"File opened"<<endl;

        string tmp;
        int i = 0;

        while( sequenceFile >> tmp )    // Counts the number of sequences (words)
        {
            i++; 
        }

        string allchains[i];   //  Creates an array of strings, to save all the words

        sequenceFile.clear();                   
        sequenceFile.seekg(0, sequenceFile.beg);  // Replaces the cursor at the beginning of the file
        i=0;

        while( sequenceFile >> allchains[i]) // Saves each word as a string in an array
        {
            cout << allchains[i] << tmp;    
            i++;
        }   

        ifs.close();
        cout<< "File closed"<<endl;
    }

    else
    {
        cout << "Error: Cannot open file" << endl;
    }

    return allchains[n]; 
} 

Here is fully functioning code: You can compare it with yours.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;


string SequenceChoice(int n, string filename){ // Function returning a string

    std::ifstream sequenceFile (filename);   //Opens the file specified on execution

    if ( sequenceFile.is_open() )
    {
        cout<<"File opened"<<endl;

        string tmp;
        int i = 0;

        while( sequenceFile >> tmp )    // Counts the number of sequences (words)
        {
            i++;
        }

        string allchains[i];   //  Creates an array of strings, to save all the words

        sequenceFile.clear();
        sequenceFile.seekg(0, sequenceFile.beg);  // Replaces the cursor at the beginning of the file
        i=0;

        while( sequenceFile >> allchains[i]) // Saves each word as a string in an array
        {
            cout << allchains[i] << tmp;
            i++;
        }

        sequenceFile.close();
        cout<< "File closed"<<endl;

        return allchains[n];            // returns the 'n'th word (n being specified when calling the function

    }

    else
    {
        cout << "Error: Cannot open file" << endl;
    }

    return NULL;


// end of the function
}


int main(int argc, char *argv[])
{
    if(argc >= 2)
        {
            string filename = argv[1];
            cout << SequenceChoice( 2, filename ) << endl; // I expect it to give me the 3rd word of a .txt file for example.
        }
        else
        {
            cout << "Error : No file" << endl;
        }

}

Thanks to your answers I managed to solve my problem, apart from some syntax errors in the code the argument filename in the function was a string, and as argv[] is an array of pointers. So it just couldn't work to consider them as the same type.

Here is a working version of the function, for those it could help :

string SequenceChoice(int n, char* argv[])
    { 

    std::ifstream sequenceFile (argv[1]);   //Opens the file specified on execution

    if ( sequenceFile.is_open() )
    {
        cout<< " .File opened. \n" <<endl;
        string tmp;
        int i = 0;

        while( sequenceFile >> tmp )    // Counts the number of sequences (words)
        {
            i++; 
        }
        cout << "  " << i << " chains in the .txt file : \n" << endl;
        string allchains[i];   //  Creates an array of strings, to save all the words

        sequenceFile.clear();                   
        sequenceFile.seekg(0, sequenceFile.beg);  // Replaces the cursor at the beginning of the file
        i=0;

        while( sequenceFile >> allchains[i]) // Saves each word as a string in an array
        {
            cout << "  --- Chain "<< i + 1 << " ---  :  " << allchains[i] << endl;    
            i++;
        }   

        sequenceFile.close();
        cout << "\n .File closed. \n" << endl;
        return allchains[n];
    }

    else
    {
        cout << "Error: Cannot open file" << endl;
        return NULL;
    }  
}

And finally the new main function :

int main(int argc, char *argv[]) {

    if(argc >= 2) 
    {
        string filename = argv[1];
        cout << SequenceChoice( 2, argv ) << endl; // Shows the 3rd word of the file, as an example.
    }
    else
    {
        cout << "Error : No file" << endl; 
    }


    return 0;
}

Thanks for your help and have a great day !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM