So actually, fname will represent for whatever the name of the file I am going to pass in the function fopen() right?
In my text_main.c, I have:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "deltas.h"
int main(int argc, char *argv[]){
if(argc < 3){
printf("usage: %s <format> <filename>\n",argv[0]);
printf(" <format> is one of\n");
printf(" text : text ints are in the given filename\n");
printf(" int : binary ints are in the given filename\n");
printf(" 4bit : 4bit binary ints are in the given filename\n");
return 1;
}
char *format = argv[1];
char *fname = argv[2];
int data_len = -1;
int *data_vals = NULL;
if( strcmp("text", format)==0 ){
printf("Reading text format\n");
data_vals = read_text_deltas(fname, &data_len);
}
else if( strcmp("int", format)==0 ){
printf("Reading binary int format\n");
data_vals = read_int_deltas(fname, &data_len);
}
else if( strcmp("4bit", format)==0 ){
printf("Reading 4bit binary int format\n");
data_vals = read_4bit_deltas(fname, &data_len);
}
else{
printf("Unknown format '%s'\n",format);
return 1;
}
printf("data_len: %d\n",data_len);
printf("%4s %4s\n","#","read");
for(int i=0; i<data_len; i++){
printf("%4d %4d\n",i,data_vals[i]);
}
free(data_vals);
return 0;
}
I am thinking "fname" may be defined in this file text_main.c. I have never used fopen() before and I am pretty new to C. I saw some posts online showing how to use fopen such as fopen("file.text","r").
You are correct about my array declaration. I must use int.
Now, I have narrowed down to 2 errors, thanks to your help:
Code:
#include <stdio.h>
int *read_text_deltas(char *fname, int *len){
FILE *fp = fopen(fname, "r");
int i, count = 0;
count = *len;
int* array = malloc(count *sizeof(int)); //Implicitly declaring library function 'malloc' with type 'void *(unsigned long)'
if (fp == NULL){
len = &(-1); //Cannot take the address of an rvalue of type 'int'
return NULL;
}
else{
while(!feof(fp)){
fscanf(fp, "%d", &array[count]);
count++;
}
}
for(i=0; i<count; i++){
printf(" \n a[%d] = %d\n",i,array[i]);
}
rewind(fp);
fclose(fp);
return 0;
}
What do these two errors mean to my logics of the program?