I have to read an undefined matrix from a text file in C language, and i want to read it line by line so that each line will be an integer array.But how do i know where is the end of a line, since i can't use "\n" as in for characters? Here is the code:
#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
using namespace System;
typedef struct
{
int *v;
int n;
}vector;
int main(array<System::String ^> ^args)
{
vector *a;
FILE* f;
int n = 15;
int i = 0;
int j,k;
if ((f = fopen("C:\\Users\\Mirelaa\\Documents\\visual studio 2013\\Projects\\MatriceNedefinita\\MatriceNedefinita\\Debug\\fisier2.in", "rt")) == NULL)
{
printf("Fisierul nu poate fi deschis!");
exit(1);
};
a = (vector *)malloc(n * sizeof(vector));
while (!feof(f))
{
a[i].v = (int*)malloc(n * sizeof(int));
a[i].n = 0;
//citeste rand
//citesti fiecare element din rand
j = 0;
while (a[i].v[j] != '\0')// wrong!!
{
fscanf(f, "%d", &a[i].v[j]);
j++;
a[i].n = a[i].n + 1;
}
for (k = 0 ; k < a[i].n ; k++)
{
printf("%d", a[i].v[j]);
printf("\n");
}
i++;
if (i == n)
{
n = 2 * n;
a = (vector *)realloc(a, n * sizeof(vector));
a[i].v = (int *)realloc(a[i].v, n * sizeof(int));
}
}
return 0;
}