Thread: have a doubt regarding files

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    have a doubt regarding files

    I am trying to write some text in to a file through the program. And then trying to print that text on to the screen. My program is running but with two warnings.
    I couldn't understand and overcome those two warning messages
    say:" Possible use of 'str' before definition in function main "
    Could someone explain what does it mean
    Code:
    #include<stdio.h>
    
    int main()
    {
    
    FILE *fp,*fs;
    char *str;
    
    fp=fopen("veer.txt","w");
    fprintf(fp,"hi Welcome to Cprogramming.");
    fclose(fp);
    
    fs=fopen("veer.txt","r");
    fgets(str,100,fs);
    printf("&#37;s",str);
    fclose(fs);
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    str doesn't have any space associated with it.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    As a pointer, str contains an address to somewhere in memory where you are trying to store data into that you are reading from the file. Since it is not initialized to anything, this address stored in str is whatever random value happens to be there. When you read data from the file into this random address you could experience many strange and wonderful (not) things. The fact that it might work is just luck. If you happen to read into some critical area your program could just as easily crash. You need to allocate space for your pointer (using malloc perhaps) or use a fixed array of some set size (100 it seems).

    Either:
    Code:
    char *str;
    str = malloc(100);
    ...
    fgets(str,100,fs);
    printf("&#37;s",str);
    ...
    free(str);
    Or:
    Code:
    char str[100];
    ...
    fgets(str,100,fs);
    printf("%s",str);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Code:
    #include<stdio.h>
     
    int main()
    {
     
    FILE *fp,*fs;
    char *str;
     
    str = (char*)malloc(101); 
    str[100] = 0; // makes it zero terminated 
    
    fp=fopen("veer.txt","w");
    fprintf(fp,"hi Welcome to Cprogramming.");
    fclose(fp);
     
    fs=fopen("veer.txt","r");
    fgets(str,100,fs);
    printf("&#37;s",str);
    fclose(fs);
     
    free(str);
    }
    there that fixes the problem, hope we get an A
    Last edited by abachler; 06-19-2007 at 12:28 PM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fgets puts the null char into the string - so you do not need an extra char and explicitely setting it to null, fgets will do it for you even if the string is longer than the buffer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by raghuveerd View Post
    have a doubt regarding files
    It's "Question" not "Doubt".
    "Doubt" is a statement about yourself only.
    A "Question" is something you ask other people.

    Please learn the difference, and tell everyone else you know to learn it too!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't cast malloc (see the FAQ), also check the return value ensuring your malloc succeeded (returns a NULL pointer if it failed).

    Code:
    char * str = NULL;
    
    /* blah */
    if((str = malloc(size)) == NULL)
    {
        perror("Malloc failed");
        return 1;
    }
    
    /* blah */
    
    free(str);
    str = NULL;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM