blitzkriegz
blitzkriegz

Reputation: 9576

Reading an array from a file

I am back to C from Java and C#. I am stuck with the following simple program attempting to read two arrays from a file with a function. Can anyone point out where am I messing up?

Compiler says: error: invalid operands to binary * (have ‘int *’ and ‘int *’)

The file format is

    4
    1   2   3   4
    23  23  14  11

My ReadFromFile function needs to fill buffers A and B from the file.

#include<stdio.h>

void ReadFromFile (const char* file_name, int *A, int *B, int *length)
{
    FILE* file = fopen (file_name, "r");
    fscanf (file, "%d", length);            
    int i;
    for(i = 0; i < length; i++)
    {
        fscanf (file, "%d", A+i); 
    }  
    for(i = 0; i < length; i++)
    {
        fscanf (file, "%d", B+i); 
    }   
    fclose (file);        
}

int main()
{
    int *A; int *B; int length;
    ReadFromFile("input.txt", A, B, &length);

    return 0;
}

Upvotes: 1

Views: 309

Answers (5)

HWende
HWende

Reputation: 1705

In your function you use A+i that accesses places in memory not allocated by you. In other words: you need a call to malloc first to get the memory.

Think about your structure here: You have address variables A and B but you never pointed them to the address of allocated memory. If you want to do this BEFORE your function you need to know the length of the array. If you want to do it IN your function, you need to pass the address of A and B to assign to it:

void ReadFromFile (const char* file_name, int** A, int** B, int* length)
{
  FILE* file = fopen (file_name, "r");
  fscanf (file, "%d", length);
  *A = (int*) malloc(length * sizeof(int))
  // now use (*A)+i

You would then change main

int* A; int* B; int length;
ReadFromFile("input.txt", &A, &B, &length);

Upvotes: 1

Bip
Bip

Reputation: 913

When calling your function you should give an address reference e.g. &A and &B

And this is how you should read a text file appropriately :)

FILE *file = fopen(file_name, "r");
while(file != EOF){
    fscanf(...)
}

EDIT:

You don't need to use double pointers. Simply initialize in main() integers Int A,B and give your method them addresses.

Upvotes: 1

Jesus Ramos
Jesus Ramos

Reputation: 23268

First of all A and B are pointers to junk, you need to allocate space if you actually want to store something (or expect a Segmentation fault or memory corruption).

It's complaining about your for loops. length is never initialized so you're sending a pointer to junk data anyway (or it's never populated in read from file).

i < length isn't valid (in that sense) because you're comparing an int value to an address or int * (which doesn't make sense). You might mean i < *length; because length is a pointer and needs to be dereferenced for its actual value.

Upvotes: 1

sarnold
sarnold

Reputation: 104020

void ReadFromFile (const char* file_name, int *A, int *B, int *length)
/* ... */
    for(i = 0; i < length; i++)
    {
        fscanf (file, "%d", A+i); 
    }

You have passed in a single integer A from main(), but here you are trying to access completely unrelated memory. Same goes for B. Did you mean to allocate A and B as arrays?

e.g. change:

int *A; int *B; int length;

to

int A[100], B[100], length;

or something similar. (Perhaps dynamically allocate the arrays once you know how many you need -- or allocate them with malloc(3) and grow them with realloc(3) if you need to.)

Upvotes: 1

Tarion
Tarion

Reputation: 17134

int main()
{
    int A; int B; int length;
    ReadFromFile("input.txt", &A, &B, &length);

    return 0;
}

Try using A and B as variables not pointers and call the function with the addresses.

I'm not sure if thats the Issue. But give it a try.

Sorry I was confused by the int pointers. I guess what you want is int[] A what is in fact the same as int* A. But then you have to allocate some memory for the array or initialize it with a given size.

Upvotes: 1

Related Questions