Read a file line by line and populate an array

Hello,

I'm trying to make the following code running, but I'm just wasting time.
I need a simple procedure for reading a text file (of some hundreds of lines where each line contains from 2 to 30 numbers separated by " " space delimiter).
I need to read each line from the file, split it into numbers, remove any leading zero if encountered, recompose the line with numbers separated by space delimiter and put it in an array.
So I will have the array corresponding to the text file but without leading zeros if the text file is containing some.
I also need to sort the array at the end.

I think if it were possible to use the getline() function on windows my code would be easier.

But I can't compile my code because I'm probably confused about pointer and assigning values to the array.

The error of this version is:

subscripted value is neither array nor pointer nor vector
universeArray[countD] = *unleadingZeroRow; //reFormat removing leading zeros if any and return the new string


but my program didn't arrive to the following instruction in any way I tried before:
fprintf(stderr, "breakpoint E \n"); //TEST

Can anybody give me some tip please?

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  // runMe.c 
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "runMe.h"


char universeArray;
int numLines = 0;


int compareSets(char *a, char *b)
{
  if(*a < *b)
    return -1;
  else {
    if(*a > *b)
      return 1;
    else
      return 0;
  }
}



char reFormat(char *str, int itemsPerLine) {
// retun the string in unleading zeros format: 01 4 09 <--to-> 1 4 9
	
	char delim[] = " ";
	char resultArr[itemsPerLine-1];
        int ind = 0;
	char *ptr = strtok(str, delim);
	*ptr = 0;

	while(ptr != NULL)
	{
    	    //resultArr[ind] = %d, ptr;
	    resultArr[ind] = ptr;
    	    ptr = strtok(NULL, delim);
	    ind++;
	}
	
	return resultArr;

}


void loadUserUniverseFile() {

    char fetchedRow;
    char unleadingZeroRow;

    #define LSIZ 128
    #define RSIZ 1000

    char line[RSIZ][LSIZ];
    char fname[20];

    

    if(universeFileIsGiven){
	FILE *uf = fopen("universeFile.txt", "r");
	
        // check if file is open
    	if(!uf) {
          
	    fprintf(stderr, "Can't open user defined file universeFile.txt");
            return(0);
    	}
	int tot = 0;

	int *Rn = 0; // number of current row for universeArray() and text file	
	int countD = 0;
	int *arrCont = line;

        while(fgets(line[countD], LSIZ, uf) != NULL)  { //read txt file line by line
	        fprintf(stderr, "start (breakpoint A) \n"); //TEST

            line[countD][strlen(line[countD]) - 1] = '\0';
	        fprintf(stderr, "breakpoint B \n"); //TEST

	    fetchedRow = arrCont[countD];
	        fprintf(stderr, "breakpoint C \n"); //TEST

	    // unleadingZeroRow = reFormat(fetchedRow, itemsPerLine); //reFormat removing leading zeros if any and return the new string
	    *unleadingZeroRow = fetchedRow;
	        fprintf(stderr, "breakpoint D \n"); //TEST

            universeArray[countD] = unleadingZeroRow; //add the new string to universeArray
	        fprintf(stderr, "breakpoint E \n"); //TEST

	    Rn++;  //Rn += 1;
                fprintf(stderr, "breakpoint F \n"); //TEST

	    countD += 1;
	        fprintf(stderr, "breakpoint G \n"); //TEST

	    arrCont++;
	        fprintf(stderr, "finish (breakpoint H)."); //TEST	
	}
	tot = Rn;

        if (line) free(line);
	
	numLines = tot - 1; // should be the number of text line of the file universeFile.txt)
	
        fclose(uf);
	
	// sort universeArray() so it will be ready for binary searchfor 
	qsort((char *) universeArray, numLines , itemsPerLine, compareSets);
    }
}


I have these declarations into a separated file

1
2
3
4
5
// runMe.h

extern char universeArray;
extern int numLines;
Last edited on
 
char universeArray;


universeArray is a char type variable.

If you need to array of character you need to declare like below:
 
char* universeArray;


There are few compile time errors in your code.
We need a constant value to define array.
Else you can do it by dynamic memory allocation. You need to use malloc().
Line : 30
1
2
3
char resultArr[itemsPerLine-1];
// Try like
char* resultArr = (char*)malloc(sizeof(char)*itemsPerLine-1);


Then you also need to change the return type for the function
1
2
3
char reFormat(char *str, int itemsPerLine) 
//To 
char* reFormat(char *str, int itemsPerLine)


Incorrect assignment.
Line : 38
 
resultArr[ind] = ptr;


Line: 61 undefined variable universeFileIsGiven

Like above there are other variable which are not defined.
And also the return type not match for few functions.

I suggest resolve all these simple compile time errors and then check for the expected output.
Last edited on
well,
universeFileIsGiven is a command line switch fetched by the code and can be 0 or 1
while my testig is set to 1.
And after appling
 
char* universeArray;

akash16's suggested correction, both, in runMe.h code and in runMe.c code

remaining faults still are:

this warning in function 'reFormat'

warning: assignment makes integer from pointer without a cast [-Wint-conversion]
resultArr[ind] = ptr;


And this error in 'loadUserUniversefile' void

error: invalid type argument of unary '*' (have 'int')
universeArray[countD] = *unleadingZeroRow; //aggiunge a universe Array la stringa di numeri non zerati
Last edited on
 
void loadUserUniverseFile()


Why are you returning ZERO from this function?
If you want return you must redefine your function like below:

 
int loadUserUniverseFile()
I would start by counting the lines in the file first.
https://www.tutorialspoint.com/c-program-to-count-the-number-of-lines-in-a-file
Once you know the number of lines you can allocate your array.
https://stackoverflow.com/questions/7652293/how-do-i-dynamically-allocate-an-array-of-strings-in-c

Then you read the file line by line, modify it and store it in your array.
Finally you have to delete your array.

Why don't you learn C++?
It would be so much easier using a vector, string and stringstream.
@akash16,
Because I don't need loadUserUniverseFile() give me back any value in response, I just need it build for me the array and let it available for using it, in a second time, when I need while running the program.
I need the universeArray() will stay in memory ready for interrogations (basically I'll just need
to check if a string it's not inside the universeArray() or, in case it is in, i'll keep the position where it has been stored)

Thanks Tomas1965, I probably will try a more performant solution later.
Now this is the road I'm trying to follow to see if the output is what I expect.


My suggestion here is,
Instead of writing code for all problem statement and build, you should break your problem statement into smaller part and try to solve one after other.

For below problem:
"
I'm trying to make the following code running, but I'm just wasting time.
I need a simple procedure for reading a text file (of some hundreds of lines where each line contains from 2 to 30 numbers separated by " " space delimiter).
I need to read each line from the file, split it into numbers, remove any leading zero if encountered, recompose the line with numbers separated by space delimiter and put it in an array.
So I will have the array corresponding to the text file but without leading zeros if the text file is containing some.
I also need to sort the array at the end.
"

Like,
1. Open file into read mode.
2. Read one line from file. i.e. string line
3. convert line which have space separated numbers into array of string.
4. convert array string into array of integers. (by doing this leading zeros may remove automatically, if not you can do it manually as well)
5. Once above steps successfully done for one line,
put steps 2 to 4 into a loop and repeat the same for all lines in file.
6. Finally when you get all numbers into a single integer array, you can sort the array using std::sort() or qsort() function.

Hope, This will help you to reach final solution.

Topic archived. No new replies allowed.