Thread: Problems with Reading and copying string from txt to console

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    7

    Problems with Reading and copying string from txt to console

    As mentioned above I have a problem with the code below

    Code:
    printf("\n**ID Search**");
    		printf("\nEnter ID:");
    		scanf("%s", &id2);
    		fp = fopen("registration.txt", "r");
    
    
    		while (fscanf(fp, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", &id, &name, &gender, &coursename, &start_d, &end_d, &fee) != EOF)
    		{
    			same = strcmp(id, id2);
    			if (same == 0)
    			{
    				printf("\n%s", name);
    				printf("\n%s", id);
    				printf("\n%s", gender);
    				printf("\n%s", coursename);
    				printf("\n%s", start_d);
    				printf("\n%s", end_d);
    				printf("\n%s", fee);
    
    
    
    
    				diff++;
    			}
    			else (diff == 0);
    			{
    				printf("ERROR\n");
    			}
    
    
    		}
    
    
    		fclose(fp);
    		system("pause");
    		system("cls");
    		return mainmenu();
    when I Use it it directly prints Error and exits with a code

  2. #2
    Registered User
    Join Date
    Jan 2020
    Posts
    7
    Not that this is a part of my code and all int have been initialized from the start

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    834
    The code you've posted wouldn't even compile if it was part of a complete program (e.g., line 25). Post the smallest program you can create that demonstrates the problem. Often in creating such an example you will find the error yourself. If not, we will have something objective to look at and run. You should also post an example input file.
    Last edited by john.c; 01-08-2020 at 12:30 PM.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  4. #4
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    240
    this is bonkers...

    Code:
    if (same == 0)
                {
                    printf("\n%s", name);
                    printf("\n%s", id);
                    printf("\n%s", gender);
                    printf("\n%s", coursename);
                    printf("\n%s", start_d);
                    printf("\n%s", end_d);
                    printf("\n%s", fee);
    
    
    
    
                    diff++;
                }
                else (diff == 0);
                {
                    printf("ERROR\n");
                }
    


    try... else if and no semicolon... i.e:

    Code:
      if (a == 1) {
        //code
      } else if (a == 2) {
        //code
      } else if (a == 3) {
        //code
      } else { 
        //code
      };
    "without goto we would be wtf'd"

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    834
    Now that I think about it, you probably just need to move that (bonkers!) else part outside the loop and make it an if.
    Code:
            printf("Enter ID: ");
            scanf("%s", &search_id);
     
            FILE *fp = fopen("registration.txt", "r");
            int cnt = 0;
            while (fscanf(fp, "%s%s%s%s%s%s%s", &id, &name, &gender,
                          &coursename, &start_d, &end_d, &fee) != 7)
            {
                if (strcmp(id, id2) == 0)
                {
                    printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n", name, id, gener,
                           coursename, start_d, end_d, fee);
                    ++cnt;
                }
            }
     
            if (cnt == 0)
            {
                printf("Error: id %s not found\n", id2);
            }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  6. #6
    Registered User
    Join Date
    Jan 2020
    Posts
    7
    Problems with Reading and copying string from txt to console-capture-jpg

    ive tried john.c but it is returning to the same error

  7. #7
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    240
    Using same == 0, or diff == 0 is weird imo.

    same should be either 1 or 0, true or false
    "without goto we would be wtf'd"

  8. #8
    Registered User
    Join Date
    Jan 2020
    Posts
    7
    tried it still dosent work

  9. #9
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    240
    What's the goal here
    "without goto we would be wtf'd"

  10. #10
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    240
    btw this is also rediculous: fscanf(fp, "%s%s%s%s%s%s%s" ...

    honestly that code posted is just not very well structured... so it would be better if you just stated your goal imo, atleast for i
    Last edited by Structure; 01-08-2020 at 02:36 PM.
    "without goto we would be wtf'd"

  11. #11
    Registered User
    Join Date
    Jan 2020
    Posts
    7
    the goal is to try and read a txt file and print the content in the console
    searching the id, it should display the the information for that specific id on the console

  12. #12
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    240
    the goal is to try and read a txt file and print the content in the console
    searching the id, it should display the the information for that specific id on the console


    could you maybe show me what a record looks like ?
    "without goto we would be wtf'd"

  13. #13
    Registered User
    Join Date
    Jan 2020
    Posts
    7
    Problems with Reading and copying string from txt to console-capture2-jpg

  14. #14
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    240
    interesting structure... let me have a go

    bbiab
    "without goto we would be wtf'd"

  15. #15
    Registered User
    Join Date
    Dec 2017
    Posts
    834
    Quote Originally Posted by Structure View Post
    btw this is also rediculous: fscanf(fp, "%s%s%s%s%s%s%s" ...
    Why?
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems copying C-code into folder
    By kiwifreak3 in forum Windows Programming
    Replies: 1
    Last Post: 05-12-2012, 10:37 AM
  2. Problems with copying strings
    By Veneficvs in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2010, 07:40 AM
  3. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  4. Replies: 1
    Last Post: 10-31-2005, 11:36 AM
  5. String Copying problems.
    By Red Army in forum C++ Programming
    Replies: 4
    Last Post: 06-04-2002, 05:16 PM

Tags for this Thread