0

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.

Community
  • 1
  • 1
Reblochon Masqué
  • 55
  • 1
  • 1
  • 10
  • 3
    Please post the error message in the same way you post code, not an offsite link to a picture of an error message. – Mike Kinghan Dec 17 '16 at 16:58

4 Answers4

3

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.

Community
  • 1
  • 1
Shiv
  • 1,912
  • 1
  • 15
  • 21
0

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]; 
} 
nikau6
  • 922
  • 9
  • 16
0

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;
        }

}
Fedorov7890
  • 1,173
  • 13
  • 28
  • Thanks for your quick answer, I still got a few errors after making corrections to my code thanks to yours, and finally found out that it came from the fact that I was using a string in my function's arguments instead of a pointer similar to `char *argv[]`. So I directly replaced `string filename` by `char *argv[]` and used `std::ifstream sequenceFile (argv[1]);`. Then in the main, calling `SequenceChoice( 2, argv )` just works fine. Thanks for your help ! – Reblochon Masqué Dec 17 '16 at 17:49
0

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 !

Reblochon Masqué
  • 55
  • 1
  • 1
  • 10