Thread: problems with program that read's notes from file [Linux]

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    4

    Question problems with program that read's notes from file [Linux]

    So, I've been book learning some C, and so to test what I learned, I wanted to make a program that writes into a file with a 4 digit password, and then a program that reads said file, and only gives back notes with the password you put into the command.

    I've gotten the writing program to work fine, but where the problem occurs, is in my reader program.

    When I first made the program, it printed the first note with that password, but not the second one. But after trying to fix that, I somehow broke it more, and now it won't print any of the notes, :P.

    some other stuff I think I should mention: I'm using a linux. it's a command line program. The test password's I've been using are 5454, which should read two notes, and 5453, which reads one note that was written after the two 5454's. I wrote the passwords as integers into the file, and like I said before, it worked on reading the note, until I broke it trying to fix it, :P.

    I'd put part of the code into the code tag, but I don't really know whats causing it, so I don't really know what to put in there, besides the whole program, which is why I just attached the files. I'm sorry that I can't exactly point you in the right direction, so I'd understand if you just wanna skip over this post and not dig through a decent chunk of code, .

    Finally, I attached a .txt file onto the post that has the passworded stuff in there, but I'm not sure if the passwords remained intact from crossing between a plain old file and a .txt file, so you might wanna use "hexdump -C" and see if the first part that gives random characters returns the values for decimal 5454 and decimal 5453.

    (shoot, this post was way longer then I meant it to be.)
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Sep 2018
    Posts
    4

    Fixed it!

    So, updating my on here, I found the problem(s), and because this form is a learning tool, I decided to document them here. So the first one, where it wasn't printing anything. After coming back and looking for half another half hour, I traced the problem back to the if statement that checks whether or not length was returned as -1, using this statement

    Code:
        if(length = -1) //If end of file is reached
            return 0; //return 0.
    Now, if your an expert programmer looking at this, you might already see my problem. (and thank you for gracing this post with your presence, expert programmer.)

    because everything in the if statement is still read and executed like normal, my program turned length into -1, instead of checking to see if it was -1. and because it succeeded in doing this, it went into the if statement, where it returned 0.

    The fixed code looks like this:

    Code:
        if(length == -1) //If end of file is reached    //Originally forgot to put == instead of =, causing nothing to print
            return 0; //return 0.
    While this fixed this part of the program, I had an issue with my program where it now read ALL of the notes in the text file. I began checking my work, and yet another 30 minutes into it, I found the culprit.

    You see, my program finds the Password in the file by doing a
    Code:
    while(user_pass != note_pass)
    Statement. This is the full part of that.

    Code:
        while(note_pass != pass) {
            if(read(fd, &note_pass, 4) != 4) //if end of file reached
                return -1;
            if(read(fd, &byte, 1) != 1) //if end of file reached
                return -1;
    
    
        } //test closer
    
    
            byte = length = 0;
            while(byte != '\n') {
                if(read(fd, &byte, 1) != 1) //if Byte is not read
                    return -1; //return end of file code
                length++;
            }
    
    
    //        printf("[DEBUG] pass = %d note_pass = %d\n", pass, note_pass);
    //        printf("[DEBUG] Note found! length = %d\n", length);
    
    
            lseek(fd, length * -1, SEEK_CUR);
            return length;
    //    }  //old end of while. included the return length statement, so it returned the note no matter the pass. New end of while is at test closer
    as you can see by the commented out curly bracket, My old end of the while statement was past the return command, so no matter what the password was, it printed.

    If anyone wants to see this is closer detail, I put the new source code attached, so you can look at the final product.
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-21-2017, 02:00 AM
  2. Are the musical notes valid in this program?
    By HelpfulPerson in forum C Programming
    Replies: 3
    Last Post: 07-22-2013, 08:06 AM
  3. Replies: 3
    Last Post: 10-31-2011, 04:33 AM
  4. FILE and strtok problems (C Linux)
    By xaykogeki in forum C Programming
    Replies: 4
    Last Post: 04-12-2010, 04:50 AM
  5. read from file problems
    By paperbox005 in forum C++ Programming
    Replies: 6
    Last Post: 10-09-2004, 09:55 AM

Tags for this Thread