Error while trying to output values.

I'm trying to assign the values in the file to a char.
But what's odd was the SIGSEGV error in the infile.getfile();
I'm not sure why.

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
/**

Name: William Lee
Compiler: GNU GCC Compiler
Ass#4

*/

#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cctype>
#include <string>


using namespace std;

void readthefile(const int, ifstream&, char*[], int);
void selectionSort(const int, string, ifstream&);

int main(){

    const int unsorted_words = 27905;
    ifstream infile;
    int i = 0;
    char **array2 = new char*[unsorted_words];
    infile.open("unsorted_words.txt");
    if (!infile){
        cout << "File not found" << endl;
    }
    else{
//        readthefile(unsorted_words, infile, array2, i);
    while(infile.getline(array2[i], 100000000)){
        cout << array2[i] << endl;
        i++;
    }
        delete [] *array2;
        infile.close();
    }
    return 0;
}

//void readthefile(const int unsorted_words, ifstream &infile, char *areee2[], int i){
//    while(infile.getline(areee2[i], unsorted_words)){
//        cout << areee2[i] << endl;
//        i++;
//    }
//}

//void selectionSort(const int size_of_array, string areee[], ifstream& file)
//{
//    int minIndex;
//    for (int i = 0; i < size_of_array - 1; i++)
//    {
//        minIndex = i;
//        for (int j = i+1; j < size_of_array; j++)
//        {
//            if (areee[j] < areee[minIndex])
//            {
//                minIndex = j;
//            }
//        }
//        if(minIndex != i) {
//            swap(areee[i],areee[minIndex]);
//        }
//    }
//}
//
// char array3[] = "HAVE A NICE DAY";
//    printStringBytes(array3,strlen(array3)+1);
//    ptr2char = strtok(array3,"ABC");
//    cout << ptr2char << endl;
//    printStringBytes(array3,sizeof(array3));
//    ptr2char = strtok(NULL,"ABC");
//    cout << ptr2char << endl;
//    printStringBytes(array3,sizeof(array3));
//    while (ptr2char != NULL)
//    {
//        ptr2char = strtok(NULL,"ABC");
//        cout << ptr2char << endl;
//        printStringBytes(array3,sizeof(array3));
//    }
//}
//
At line 28, you allocate an array of pointers to char. But each of those pointers is itself uninitialised, it contains garbage. In order to use the pointer, first allocate some memory for it to point to. One way to proceed is to first read a word from the file. Find the length of the word, allow 1 extra for the terminating '\0' and allocate it with new[]. Then copy the word into the array.

I don't know what the number 100000000 was supposed to be at line 35. It should be the size of the character array (or less). But you didn't even have a character array, let alone one with space for 100 million characters.

At the end, each new[] must have a corresponding delete[].

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
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cctype>
#include <string>

using namespace std;

int main()
{
    const int unsorted_words = 27905;
 
    ifstream infile("unsorted_words.txt");

    if (!infile)
    {
        cout << "File not found" << endl;
        return 1;
    }

    char **array2 = new char*[unsorted_words];
        
    int i = 0;
    
    char buffer[80];
    
    while ( i < unsorted_words &&  infile.getline(buffer, sizeof(buffer))  )  
    {
        array2[i] = new char[ strlen(buffer) + 1]; // add 1 to allow for null terminator
        
        strcpy(array2[i], buffer);
        
        cout << array2[i] << endl;
        i++;
    }
    
    infile.close();

    int count = i; // number of words actually read from file.
    
    cout << "Number of words read = " << count << '\n';
    
     
    // release allocated memory when finished
    for (int i=0; i<count; ++i)
    {
        delete [] array2[i];
    }
    
    delete [] array2;
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.