Thread: Program reading data of previous 'record' into the new one to be added

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    6

    Question Program reading data of previous 'record' into the new one to be added

    Basically,
    I have a program that reads a file and stores the information into a struct which then goes into a linked list. When I close the program it will write to file.
    When I open the program with an existing 'record' in the txt (file to be read), add a new record, and print the contents of that new record, It displays information of the initial entry along with the newly added one appended to the end. However, if I were to add another record without closing the program, it is reading the correct information and storing the correct information. I'm puzzled as to why this problem only occurs for the first addition onto an existing list of records.

    Problem only occurs when:
    1.) There is an existing record to be read from in the txt file
    2.) It is the first record to be added on top of the other existing records.

    I also found that the data fields that got read wrong were being processed by 'fgets'.

    My program is very large. If it is 6 files can I still post it here?

    P.S : Works on IDE not on UNIX
    Last edited by Xirol; 03-15-2016 at 04:41 AM.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    If you post 6 files I doubt anybody will be able to read and understand it. Try to reduce your problem to a bare, necessary minimum - you are very likely to find the error yourself when doing that. Also, if you suspect any parts of your program to be the cause of the problems, post them with a description of what you expect them to do. Keep in mind that posting more than 100-200 lines of code is unlikely to be read and/or analyzed by anyone.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    6
    I see. Well it's a bit complicated since there are so many function calls that lead one to the other, narrowing the code down wouldn't suffice. If there is any brave soul out there willing to help me out, I'd really really really appreciate it. But @kmdv I understand, thanks for the advice.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Xirol
    If it is 6 files can I still post it here?
    Yes, but as kmdv noted the more code you post, the less inclined people will be to read it since they are just volunteering to help for free, so can select to help with whatever appears easier or more interesting.

    Quote Originally Posted by Xirol
    Well it's a bit complicated since there are so many function calls that lead one to the other, narrowing the code down wouldn't suffice.
    That makes it sound like your code is tightly coupled. Have you tried stepping through your code with a debugger?

    When you say "Works on IDE not on UNIX", perhaps we could suspect a line ending problem. Are you running on Windows?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    6
    I'm running on a MAC. I'll post a portion of the cod

    OPTION TO ADD A NEW RECORD

    Code:
        int accountno;
        char name[25];
        char address[80];
        int addresscap = 79;
        int yearofbirth;
        char yearinput[10];
        char info[100];
        char filename[] = "data.txt";
    
    
               printf("Please create your own account number: ");
                scanf("%s",info);
     
                while(!isInteger(info,99)){
                    printf("\nThat is not a valid account number. Only integers are allowed.\n");
                    printf("Please create your own account number:");
                    scanf("%s",info);
                }
                accountno = atoi(info);
     
                clear(); //removes lingering '\n'
     
                printf("\nPlease enter your name: ");
                fgets(name, 24, stdin);
     
                printf("\nPlease enter your address:\n");
                printf("To finish your input, make a new line after your input and enter 'quit':\n");
                getField(address,addresscap); //Reads multiple lines until user inputs 'quit' 
     
                printf("\nPlease enter your year of birth: ");
                scanf("%s",yearinput);
     
                while(strlen(yearinput) != 4 || !isInteger(yearinput,10)){
                    printf("\nNot a valid year of birth. Please try again: \n");
                    scanf("%s",yearinput);
                }
                yearofbirth = atoi(yearinput);
     
                addRecord(&start,accountno,name,address,yearofbirth); //Adds into singly linked list


    READ FILE CODE


    Code:
       FILE *fp = NULL;
        fp = fopen(filename,"r");
     
        int accountno = 0;
        char name[25] = { 0 };
        char address[80] = { 0 };
        int yearofbirth = 0;
        char line[80] = { 0 };
        int opened = 1;
     
        if (fp == NULL){
     
      opened = 1;
     
        } else {
     
      opened = 0;
     
      while (fscanf(fp, "%d\n", &accountno) > 0) {
     
         printf("accountno: %d\n",accountno);
               
         fgets(name, 25, fp);
    
         while (fgets(line, 80, fp)) {
             if (strcmp(line, "*\n") == 0) {
               strncpy(line,"",sizeof(line)-1);
               break;
              }
              strncat(address, line, 79 - strlen(address));
          }
     
    fscanf(fp, "%d", &yearofbirth);
     
                addRecord(start, accountno, name, address, yearofbirth); //Reads each piece of data in the file and adds it to the linked list
     
                    strncpy(line,"",sizeof(line)-1); // Used these to try to clear out the array
                    strncpy(address,"",sizeof(address)-1);
                    strncpy(name,"",sizeof(name)-1);  
            }
        }
     
        if (opened != 1){
        fclose(fp);
        }
     
        return opened;
    }
    Last edited by Xirol; 03-15-2016 at 06:25 AM.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should post code in code tags.

    If I am understanding correctly, the issue can be described as follows:

    • Program is run
    • Records are read from a file and stored in a list
    • A new record is added to the list
    • You print that new record [error, the first record in the list is also printed]
    • Another new record is added to the list
    • You print that new record [success, only that record is printed]


    If you haven't done so already, the first thing I'd suggest is to write a function that prints the entire list, from head to tail. Call this after you add a new record, and make sure there are no problems with the list itself. (It doesn't sound like this is the case, but you should start by ruling this out.)

    If the list appears to be okay, then the problem might be in the function that prints only the latest record. As suggested, you should start looking at this function with your debugger. You can try posting the code for that function, but it might not be possible to find a mistake by looking at that function alone.

  7. #7
    Registered User
    Join Date
    Mar 2016
    Posts
    6
    I have made a function that does just that (printing them all), and it comes out nicely except for the FIRST newest addition to a linked list that already has records in it. By print do you mean, my write to file function?

    The situation:

    1.) Run program with at least one record to be read on the data.txt file
    2.) Add a record
    3.) Print the record and it contains information from previous data.

    INITIAL txt before running program:

    1111 //Account number
    Person's Name //Name
    Their Street //Address
    * // Marks the End of Street
    1990 // Year of Birth


    txt AFTER addition of a record

    1111
    Person's Name
    Their Street
    *
    1990 //END OF RECORD 1
    2222 //START OF RECORD 2
    1111 //START of RECORD 2'S NAME (yes this is what is under name)
    Person's Name
    New Person's Name
    Their Street
    *
    1990 //END
    et //START of RECORD 2'S address
    *1990
    Person's Name
    New Person's Name
    Their Street
    *
    1990 //END
    1990 // RECORD 2'S year of birth


    Its so aggravating.
    All my print functions do not alter the information at all. I've tested it because when I add another record after the glitchy one, that prints fine. The only functions that get called throughout this whole process is addRecord/readFile(beginning of program)/and running the option to add a record
    Last edited by Xirol; 03-15-2016 at 06:37 AM.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Xirol View Post
    By print do you mean, my write to file function?
    Sure, let's start with that.

    Also post your struct declaration.

  9. #9
    Registered User
    Join Date
    Mar 2016
    Posts
    6
    I appreciate the help guys, but it's too complicated to really put into words. Its much easier to understand if someone were to run the program. I'm going to try to fix it on my own. Again, thanks for all the help!

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Feel free to upload the 6 files, and I'll take a look at them. I can't promise anything, but maybe I'll spot something after a cursory look.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. append data at end of previous memcpy
    By metroflex in forum C Programming
    Replies: 5
    Last Post: 08-23-2009, 09:26 AM
  2. Does realloc save the previous buffer data?
    By Niara in forum C Programming
    Replies: 6
    Last Post: 07-23-2008, 04:40 AM
  3. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  4. Replies: 1
    Last Post: 10-22-2005, 05:28 AM
  5. Replies: 20
    Last Post: 06-12-2005, 11:53 PM