Matthias NKR
Matthias NKR

Reputation: 35

Read and write .txt files in C

I'm trying to make this code that reads a .txt file from the computer, copies it's information to an usable declared matrix and then prints, not the file, but the matrix it generated reading the file. I cant seem to make it work for some reason. It gets to the reading phase but it's failing to store the data.

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

void main (void)
{
    float matriz[20][20];

    NOME_ARQUIVO: printf("Insira o nome do arquivo que contem as informacoes da secao U \n");

    char na[100];

    FILE *input;

    gets(na);

    printf("Nome do arquivo procurado : %s\n",na);
    input = fopen(na , "r");

    if (input == NULL)
        {
            printf("Arquivo inexistente ou incompativel.\n");
            goto NOME_ARQUIVO;
        }
    else{

    int i , j ;
    for (i=0;i<20;++i)
        for (j=0;j<20;++j)
            fscanf(na,"%f",&matriz[i][j]);
    fclose(input);

    for (i=0;i<20;i++)
        printf("\n");
        for (j=0;j<20;j++)
            printf("%f",matriz[i][j]);
            }
}

Upvotes: 0

Views: 110

Answers (1)

Rob
Rob

Reputation: 15168

You opened the file for reading, not writing. The file is now input but you don't refer to input anywhere else.

Open the file with input = fopen(na , "r+"); to both read and write but you don't show any code that writes to the file input.

Upvotes: 1

Related Questions