fseek() changing an unrelated variable?

This is a discussion on fseek() changing an unrelated variable? within the C Programming forums, part of the General Programming Boards category; Hi all, Sorry, I'm a bit new at this, but I'm trying to get a certain function to work on ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Whitby, Canada
    Posts
    2

    fseek() changing an unrelated variable?

    Hi all,
    Sorry, I'm a bit new at this, but I'm trying to get a certain function to work on my Mac (using XCode on Leopard), and I've discovered some weird behaviour. I've atomized the issue down to this code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (int argc, const char * argv[]) {
        char* string;
    	FILE* filename;
    	
    	filename = fopen("sample.pdf", "rb");
    	printf("Enter a string: ");
    	scanf("%s", string);
    	
    	printf("Pass before fseek() = %i\n", strlen(string));
        fseek(filename,0,SEEK_END);
    	printf("Pass after fseek() = %i\n", strlen(string));
    	
    	//printf("String is %i characters long", strlen(string));
        return 0;
    }

    I've been testing with Windows and Mac, but I really need to get this working on the Mac. What happens is, this program takes a string -- it's meant to be a password. However, after doing an fseek operation on a file, it gets very whacky. I'm testing length of the string, and depending on its original length, it either decrements by one, or goes to zero altogether. Here's some sample output:

    Code:
    Password: asdf
    Pass before fseek() = 4
    Pass after fseek() = 3

    Code:
    Password: dkdkd
    Pass before fseek() = 5
    Pass after fseek() = 0
    It works fine in Windows. Any suggestions?

    Thanks,
    Aaron.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm utterly surprised that IT "WORKS" at all. By all means, this code should crash.

    The variable string is a pointer to char that hasn't been given a value, so when you do scanf() you are reading into a undefined memory location - you are just "lucky" enough that it doesn't crash immediately [actualy, if I make a mistake where I use an uninitialized pointer, and it does happen, I prefer it when it crashes immediately, rather than "sort of working but behaving strange", which is the situation you are in].

    If you allocate some memory for your "string", you will most likely be able to use it correctly too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    In other words, storing data into a char* isn't going to work. You need to either use an array
    Code:
    char string[LENGTH];
    or dynamically allocate memory for yourself.
    Code:
    char *string = malloc(length);
    /* ... */
    free(string);
    Note that with this you have to put an upper limit on the number of characters the user can enter. (When calculating sizes, don't forget to take the NULL into account.) You can also resize the allocated memory as required, to allocate just the right amount of memory (or maybe a little more) for your string, but it's a little complicated and I'm not going to explain it right now. (Look into realloc() if you're interested.)

    Pedantic note: your FILE* filename variable isn't really a "filename". I usually refer to FILE* pointers as "file pointers" (and UNIX-style int ones as "file descriptors"), while a char* filename like "text.txt" is a "filename".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Location
    Whitby, Canada
    Posts
    2
    Aha! Right you are. Thanks so much for your help, and your rapid replies!

    Cheers,
    Aaron.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing a variable
    By matthew82 in forum C Programming
    Replies: 18
    Last Post: 02-06-2009, 01:16 PM
  2. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  3. Changing variable
    By Mithoric in forum Windows Programming
    Replies: 4
    Last Post: 03-30-2004, 08:45 PM
  4. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM
  5. Why does my variable keep changing to -858993460?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2001, 10:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21