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