I was hoping if you can help me with some input and 2 dimensional arrays for this method of a program I am writing for an assignment. The programming language is C I got some input parameters:
FILE * ifp = input file pointer, opened up in main, opened a txt file with a format of 3 strings per line
char ** firstTokens, middleTokens = 2 dimensional char arrays, I want them to hold the first two strings minus expected punctuation at the end of each string (such as commas and periods). Planned to be an output parameter.
char * lastLetter = The first letter of whatever string is in the last column per line in the txt file. Planned to be an output parameter.
int numberOfLines = number of lines I’m expecting to read from the text file
This is the call to the method in my main function:
readLine(ifp, firstTokens, middleTokens, lastLetter, numberOfLines);
I think my problem is with getting the firstTokens and middleTokens correctly, as I’m always getting compiler errors and segmentation faults in my edits. Some help/clarifications to the errors would be greatly appreciated.
void readLine(FILE * ifp, char ** firstTokens, char ** middleTokens, char* lastLetter, int numberOfLines)
{
char* tempFirst;
char* tempMiddle;
char* tempLast;
char delim[4];
delim[0] = '.';
delim[1] = '\0';
delim[2] = '\n';
delim[3] = ',';
int i;
for(i = 0; i < numberOfLines; i++)
{
fscanf(ifp, "%s %s %s", tempFirst, tempMiddle, tempLast);
*firstTokens[i] = strtok(tempFirst, delim);
*middleTokens[i] = strtok(tempMiddle, delim);
lastLetter[i] = tempLast[0];
}
}
You’ll need to allocate storage for
tempFirst,tempMiddle, andtempLast.fscanfwrites to these pointers, assuming you’ve provided sufficient memory at them.