Reputation: 21
I am allocating memory for an array. The data is coming from 3 different text files that I am merging and alphabetizing. when I try to fscanf from the files I get an error saying that my array is type int when I definitely. declared type char
I have been googling stuff to try and fix it and it is not helping. I tried doing a rewind() because I was thinking maybe the position indicator might be at the end of the file. The size of the array is the number of lines in the .txt files. The .txt files just have a word per line. I took out the counting lines function out to help clean out unimportant code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int addOn(FILE *file, int size);
char **array;
int height=0;
int count2 = 0;
int main()
{
FILE *file0;
FILE *file1;
FILE *file2;
int i;
int sz0, sz1, sz2;
int totalsz;
//open files
file0 = fopen("american0.txt", "r");
file1 = fopen("american1.txt", "r");
file2 = fopen("american2.txt", "r");
//getting size of the files
sz0 = countLines(file0);
sz1 = countLines(file1);
sz2 = countLines(file2);
totalsz = sz0+sz1+sz2;
printf("%d", sz0);
//initializing array
array = (char **)calloc(totalsz, sizeof(char *));
for(i = 0; i < totalsz; i++)
{
array[i] = (char *)calloc(50, sizeof(**array));
}
addOn(file0, sz0);
addOn(file1, sz1);
addOn(file2, sz2);
fclose(file0);
fclose(file1);
fclose(file2);
for(i = 0; i <totalsz; i++)
{
free(array[i]);
}
free(array);
return 0;
}
int addOn(FILE *file, int size)
{
char str[50];
for(int height = 0; height < size; height++)
{
for(int width = 0; width < 50; width++)
{
fscanf(file, "%s", array[count2][width]);
printf("%s", array[count2][width]);
count2++;
}
}
}
I expect to see each word print out after I put it in the array, but I only get this error:
warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
fscanf(file, "%s", array[count2][width]);
^
warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("%s", array[count2][width]);
Upvotes: 1
Views: 33
Reputation: 21
I just figured it out after I posted it. Of course. when I am trying to put a word into an array in the addOn(), I am accessing each char rather than letting fscanf doing that.
Should be:
fscanf(file, "%s", array[count2];
printf("%s", array[count2]);
Right now if you are looking at this, it only works for the firt addOn that runs because I haven't adjusted what I am sending into addOn the 2nd and 3rd time it runs which hsould be a quick fix.
Upvotes: 1