How to assign a text file to a c-string array

When the user enters an option, the file would be sent to the function that returns the amount of characters.

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
  #include <fstream>
#include <cstring>
#include <iostream>
using namespace std;

void printMenu(){
     cout <<"Spell Checker for the Different Languages" <<endl;
     cout <<endl;
     cout <<"1.   Load Dictionary [English, French, Spanish]"<<endl;
     cout <<"2.   Load Passage"<<endl;
     cout <<"3.   Display Passage"<<endl;
     cout <<"4.   Spell Check Passage"<<endl;
     cout <<"5.   Exit"<<endl;
     cout <<endl;
     cout <<"Please enter your choice (1-5): "<<endl;
}

int loadWords(char dictionary[], char file[]){
    ifstream in;
    int i;
    char ch;
    in.open(file);
    if(!in.is_open()){
        cout<<"Error opening file..exiting"<<endl;
        return 0;
    }
       i=0;
        in>>skipws;
        in>>ch;
        while(!in.eof()){
            dictionary[i] = ch;
            i = i + 1;
            in>>ch;
    }
    dictionary[i]='\0';
    in.close();
	return i;
}

int main(){
    char dictionary[80000];
    char file[80000];
    int choice;
    printMenu();
    cin>>choice;
        if(choice==1){
            char op1;
            cout<<"Enter E for English, S for Spanish, F for French or M for Menu: ";
            cin>>op1;
                if(op1=='E')
                    file="dictionary-en.txt";
                else if(op1=='S')
                    file="dictionary-es.txt";
                 else if(op1=='F')
                    file="dictionary-fr.txt";
                else if(op1=='M'){
                    printMenu();
                    cin>>choice;
                }
            int numWords=loadWords(dictionary, file);
            cout<<numWords<<" words read successfully."<<endl;
        }

    return 0;
}
Why are you messing around with C-style arrays? Use std::string

To read the contents of a text file (including white space) as a c-style string:
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
#include <iostream>
#include <fstream>

std::size_t get_file_contents( const char* file_name, char text[], std::size_t sz )
{
    std::size_t nchars_read = 0 ;

    if( text && sz>0 ) // if the array has space for at least one char
    {
        if( std::ifstream file{ file_name } ) // and if the file was opened successfully
        {
            char ch ;
            // copy up to a maximum of sz-1 characters read from the file into the array
            while( nchars_read < (sz-1) &&  file.get(ch) ) text[ nchars_read++ ] = ch ;
        }

        text[nchars_read] = 0 ; // finally, null terminate the array
    }

    return nchars_read ; // return the number of characters that were read
}

int main()
{
    {
        // the array is not large enough to hold the entire contents of the file
        char text[35] ;
        std::cout << "sizeof(text) == " << sizeof(text) << '\n' ;
        std::cout << get_file_contents( __FILE__, text, sizeof(text) ) << " chars were read\n\n" ;
        std::cout << text << '\n' ;
    }
    std::cout << "\n\n\n" ;
    {
        // an array that is big enough to hold the entire contents of the file
        char text[35000] ;
        std::cout << "sizeof(text) == " << sizeof(text) << '\n' ;
        std::cout << get_file_contents( __FILE__, text, sizeof(text) ) << " chars were read\n\n" ;
        std::cout << text << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/4983c610f09cb492
Topic archived. No new replies allowed.