0

I have a problem, I was trying to make a C program taht read a file with 50 lines and 11 columns, the problem is that this file is completely made of strings and I made this:

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

typedef struct
{
char nome[100];
char dica[10][300];
} PAIS;

void main()
{
int i;
i = 0;

PAIS paises[50];
char nome[30];

FILE *arq;

arq = fopen("Dicas3.txt", "r");

//fscanf(arq,"%s", nome);
//printf("%s", nome);

while(!feof(arq))
{

    fscanf(arq,"%s %s %s %s %s %s %s %s %s %s %s", paises[i].nome, paises[i].dica[0][0], paises[i].dica[1][0], paises[i].dica[2][0], paises[i].dica[3][0], paises[i].dica[4][0], paises[i].dica[5][0], paises[i].dica[6][0], paises[i].dica[7][0], paises[i].dica[8][0], paises[i].dica[9][0]);

    printf("%s %s %s %s %s %s %s %s %s %s %s", paises[i].nome, paises[i].dica[0][0], paises[i].dica[1][0], paises[i].dica[2][0], paises[i].dica[3][0], paises[i].dica[4][0], paises[i].dica[5][0], paises[i].dica[6][0], paises[i].dica[7][0], paises[i].dica[8][0], paises[i].dica[9][0]);
    i++;
}



    system ("PAUSE");
}

The program compile, but the program dosn´t work. Please, could someone show me how to do a program that read a matrix 50x11 and print in the window this matrix?

the matrix is in this file (is in portuguese, but this matrix is made os strings):

https://drive.google.com/file/d/1BLhGSHIx69Ycrasgtl4lKyxxKPRlSAv2/view?usp=sharing

  • 1
    *but the program dosn´t work.* What does that mean? – DeiDei Dec 01 '17 at 12:46
  • please provide an example of the file Dicas3.txt. – user803422 Dec 01 '17 at 12:46
  • What do you get? What do you expect? What do you feed is better explained by giving some examples. – alk Dec 01 '17 at 12:46
  • 1
    To read s tring with `fscanf`, you must provide a pointer to the string space. So replace `paises[i].dica[0][0]` with `paises[i].dica[0]`. Same for `printf`. – Paul Ogilvie Dec 01 '17 at 12:51
  • `while (!feof` is wrong. `feof` will be true _after_ a read operation failed. You need to know _when_ the read operation fails. Use `while (fscanf(..)>0)` – Paul Ogilvie Dec 01 '17 at 12:53
  • You also may want to limit the number of lines and the number of characters per field that will be read. So `while (nLines<50 && fscanf(...` and use format specifiers `"%30s %300s ...` – Paul Ogilvie Dec 01 '17 at 12:59
  • 2
    Please read about [why `while(!feof(file)) {}` is always wrong.](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – ad absurdum Dec 01 '17 at 13:09
  • Also, always check return value of any scanf function call. – hyde Dec 01 '17 at 15:19
  • `void main` is deprecated for decades, please don't use that... – Serge Ballesta Dec 01 '17 at 16:30

1 Answers1

1

Dont't use feof() find out reason for Why is “while ( !feof (file) )” always wrong?

If you know well in advance about no of lines and columns, since in each line 11 columns/strings are there, read line by line from file using fgets() instead of fscanf(). My suggestion is instead of local array use dynamic array if you want to make it generic solution..

What nome[] contains, not clear ? using fscanf() :

typedef struct
{
    char nome[100];
    char dica[10][300];
} PAIS;
int main()
{
    PAIS paises[50];
    char nome[30];
    FILE *arq;
    arq = fopen("Dicas3.txt", "r");
    if(arq == 0) {
        printf("file not present:\n");
        return 0;
    }
    int i=0,j=0;
    char ch;
    while(fscanf(arq,"%s",paises[i].dica[j])>0) {
        printf("%s  ",paises[i].dica[j]);
        j++;//column
        if((ch = fgetc(arq))=='\n')//when new lines occures, start reading from next lines, do i++
        {
            i++;//rows or lines 
            printf("\n");//manuaally put the new line or use fputc(ch,stdout)
            fseek(arq, -1, 1);//move one letter back 
            j=0;
        }
        else
            fseek(arq, -1, 1);//move to exact position
    }
    return 0;
}

I hope it helps.

Achal
  • 11,821
  • 2
  • 15
  • 37