Thread: Probem reading file, only first record is read

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    159

    Probem reading file, only first record is read

    Hello

    I'm having problems getting the name of the record saved in txt file.
    So i ask the user to choose an id and the name must be retrivied but it only works with fisrt line

    This my code

    Code:
    int loginprofsaude(struct ficha *admissao, int registo) {
    
    
    	char nint2[100];
    	int a;
    
    
    	printf("\nId: ");
    	scanf("%s", nint2);
    	fflush(stdin);
    
    
    	FILE *fadmissao;
    	fadmissao = fopen("fadmissao.txt", "r+");
    
    
    	while (fscanf(fadmissao, "%s %s %d %s %d %s %d %d %s\n", admissao[registo].nint, admissao[registo].nomecompleto, admissao[registo].idade, admissao[registo].especialidade,admissao[registo].anos, admissao[registo].localidade, admissao[registo].telefone, admissao[registo].salario, admissao[registo].horario)!= EOF) {
    
    
    		a = strcmp(admissao[registo].nint, nint2);
    		 printf("valores %d %d %d", nint2,admissao[registo].nint, a );
    
    
    		if (a == 0) {
    			printf("\nLogin ok.");
    			printf("\nName: %s.", admissao[registo].nomecompleto);
    
    
    			fclose(fadmissao);
            	return 1;
    		}
    
    
    	}
    
    
    
    
    		if (feof(fadmissao)) {
            	printf(Doesn't exist.");
        	}
    
    
    		fclose(fadmissao);
    		return 0;
    
    
    
    
    }
    my txt file has this

    1 John
    2 Maria

    So if i put 1...i get John
    If i put 2 the program exit

    any help?

    Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,580
    Your fscanf() call doesn't match the data in the file, which will cause problems.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Thanks for your info

    Can you point me to the right direction please?
    When i hit 2...
    Process returned -1073741819 (0xC0000005) execution time : 14.598 s
    Press any key to continue.
    What do i have to change then?

    thanks
    Last edited by Gil Carvalho; 01-19-2021 at 12:05 PM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,580
    As I have already hinted at, you either need to change your fscanf() call to match the data in your file, or change the data in the file to match your fscanf() call.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Quote Originally Posted by jimblumberg View Post
    As I have already hinted at, you either need to change your fscanf() call to match the data in your file, or change the data in the file to match your fscanf() call.
    Well, iv'e been trying to fix that with fgets, fread...changed from txt do dat and still no way of getting this working.

    can you help me please?

    Thanks

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,580
    Show what you've tried, your latest complete program.

    Why the switch to fgets()?

    What do you think fread() brings to the table?

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    782
    Also, please remove the call to "fflush(stdin)" fflush() is for output files only. Please read why.

    Use this instead:
    Code:
       int ch = 0;
       while ((ch = getchar()) != '\n' && ch != EOF);
    ch must be an int, not a char to handle EOF!

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Quote Originally Posted by jimblumberg View Post
    Show what you've tried, your latest complete program.

    Why the switch to fgets()?

    What do you think fread() brings to the table?
    Ok

    I have changed the way the information was saved into the file using a structure

    Code:
    struct medicos
    {
     int idMedico,telefone, salario, anos, idade;
     char localidade[100], nomecompleto[100], especialidade[100], horario[100], nint[100];
    
    
    }med;
    then i use a function to create new records

    Code:
    void criarficha() {	//Menu_2 -> 1 - Criar ficha de admissão de profissional de saúde
    
    
    	printf("\nFICHA DE ADMISSÃO DO PROFISSIONAL DE SAÚDE. ");
    
    
    	printf("\nIntroduza o número interno do profissional de saúde: ");
    	scanf("%s", &med.idMedico);
    	fflush(stdin);
    
    
    	printf("\nIntroduza o nome completo do profissional de saúde: ");
    	scanf("%[^\n]s", &med.nomecompleto, 100);
    	fflush(stdin);
    
    
    	printf("\nIntroduza a idade do profissional de saúde: ");
    	scanf("%d", &med.idade);
    	fflush(stdin);
    
    
    	printf("\nIntroduza a especialidade do profissional de saúde: ");
    	scanf("%s", &med.especialidade, 100);
    	fflush(stdin);
    
    
    	printf("\nIntroduza o número de anos de experiência do profissional de saúde: ");
    	scanf("%d", &med.anos);
    	fflush(stdin);
    
    
    	printf("\nIntroduza a localidade do profissional de saúde: ");
    	scanf("%[^\n]s", &med.localidade, 100);
    	fflush(stdin);
    
    
    	printf("\nIntroduza o número de telefone do profissional de saúde: ");
    	scanf("%d", &med.telefone);
    	fflush(stdin);
    
    
    	printf("\nIntroduza o salário, por mês, do profissional de saúde: ");
    	scanf("%d", &med.salario);
    	fflush(stdin);
    
    
    	printf("\nIntroduza o horário semanal do profissional de saúde: ");
    	scanf("%s", &med.horario, 100);
    	fflush(stdin);
    
    
    	FILE *fp;
    	fp = fopen("fichaMedicos.dat", "a+");
    	fwrite(&med, sizeof(med), 1, fp);
    	fclose(fp);
    
    
    }
    Finally when i want to check by id i use another function

    Code:
    int loginprofsaude() {
    
    
    	int r, s, avl;
    	printf("\nID: ");
    	scanf("%s",r);
    	FILE *fp;
    	if(fp==NULL)
        {
            printf("File could not open");
            exit(0);
        }
        avl = avlrollno(r);
        if (avl == 0)
            printf("This id doesn't exist\n",r);
         else
         {
          fp = fopen("fichaMedicos", "r");
          while (fread(&med, sizeof(med), 1, fp))
          {
           s = med.idMedico;
           if (s == r)
           {
            printf("\nId = %d", med.idMedico);
            printf("\nName    = %s", med.nomecompleto);
            printf("\nType    = %s\n", med.especialidade);
           }
          }
         }
    	fclose(fp);
    }
    when when i do this i have again
    Process returned -1073741819 (0xC0000005) execution time : 14.598 s
    Press any key to continue.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,580
    Please post a small sample of your input file, 10 lines or so should be adequate.

    Also you need to post a complete program.
    Last edited by jimblumberg; 01-20-2021 at 03:57 PM.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Hello

    Thanks for your help

    Sending a google drive link where you have the program and a txt file

    After running choose option 2 and put one of the id (1 to 20)

    Google Drive: Sign-in

    Thanks for helping me

    regards
    Last edited by Gil Carvalho; 01-20-2021 at 06:50 PM.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,580
    Please post a small sample of your input that actually matches your structure. For example it appears, from the criarficha() that the first field is a string but your file has an int as the first field. Then you have a telephone field that also looks like an int, that looks nothing like a real telephone number (53445), normally a telephone number will be held in a string, since it is really not a "number" but a series of codes identifying your telephone.

    The program also has quite a few warnings that you need to look into and fix. Many of the warnings are about incorrect format specifiers for the type of variable when using scanf(). The scanf() series of functions are very particular, the format specifier must always exactly match the variable type.

    In the future please post the code and your file as text in a post, don't make people go to some external site to view your stuff.


    Lastly for now stay away from fread() and fwrite(). First learn to use fgets(), scanf(), printf() first. The fread() and fwrite() functions just make it harder to understand what is happening.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading length of string from record in file
    By TexasKid in forum C Programming
    Replies: 4
    Last Post: 04-24-2012, 09:39 AM
  2. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  3. Read Each Line of File as Array Record
    By Leo2010 in forum C Programming
    Replies: 3
    Last Post: 02-23-2010, 10:13 AM
  4. Reading a record from a File
    By David101 in forum C Programming
    Replies: 2
    Last Post: 12-14-2004, 06:42 PM
  5. reading a customer record file
    By abbycat131 in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 09:15 PM

Tags for this Thread