Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The part of my project that I need help with is reading a file in, tokenizing it

ID: 3628528 • Letter: T

Question

The part of my project that I need help with is reading a file in, tokenizing it and putting the ints into a 2d array. (preferably using a pointer and malloc)

The file looks like this:

5
2: 8, 7
1: 4
7: 5, -2, 14, 3, 22, 0, -2
3: 4, 2, 10
1: 80

I am very confused about what types i need and the syntax to go about doing this. The errors i get is about the strtok line and casting in the 2d array attempt.

Please dont use any other library other than the ones i have listed.

Any advice/help is much appreciated.
Thanks!


My Code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
FILE*fp = fopen("list.txt","r"); //input file
if(fp!=NULL)
{
int len;
int i;
int j;
char temp[100];
char* token;
int lengths[len];
int** matrix;

fscanf(fp, "%d",&len); //first number: size of list
fscanf(fp, "%s",temp);
token = strtok(temp,"," || ":");
while(token!= NULL)
{
for(i=0;i<len;i++)
{
lengths[i]=atoi(token); //first int is size of list
matrix = malloc(len*sizeof(int*)); //start of 2-d array
for(j=0;j<len;j++)
{
matrix[i] = malloc(lengths[i]*sizeof(int));
matrix[i][j]=token;
printf(matrix[i][j]);
}
}
}


Explanation / Answer

here is a pretty good example of what you are trying to ask. lets see if this helps you. if it doesnt then lets try going through your program line by line. http://www.cplusplus.com/forum/beginner/6027/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote