Error on fscanf when reading a struct from a file

This is a discussion on Error on fscanf when reading a struct from a file within the C Programming forums, part of the General Programming Boards category; Hi, I am attempting to read in structs from a file, however am getting "Access violation writing location", my function ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    England
    Posts
    4

    Error on fscanf when reading a struct from a file

    Hi,
    I am attempting to read in structs from a file, however am getting "Access violation writing location", my function may be incorrect as I am struggling with this, any help appreciated!

    Code:
    char** branchvalues;
        int i,y;
    
    
     FILE* branchPtr = fopen("Struct.txt", "r");
        if (!branchPtr)
        {
            printf("*******************************");
            branchPtr = fopen("Struct.txt", "w");
            if(branchPtr !=NULL)
                printf("\nNew file %s was created", branchPtr);            
        }
            
        
        branchvalues = (char**)malloc(sizeof(struct Branch)* GROWBY);
        printf("%s", tempbranch.BranchName);
    
    
        do
        {
        i = fscanf(branchPtr, "%s%f%d", tempbranch.BranchName, tempbranch.NumberOfFiles, tempbranch.DateCreated);
        if(i!=0)
        {
            printf("%s%f%d", tempbranch.BranchName, tempbranch.NumberOfFiles, tempbranch.DateCreated);
            y++;
        }
        }
        while (i!=0);
    
    
        
        
        fclose(branchPtr);
    
    
     }
    Last edited by faustian_1; 10-23-2011 at 11:13 AM.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Your posted code cannot compile.
    Please post correct code.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    England
    Posts
    4

    function code

    Hi, Here is the code I am compiling. Again thanks for the help
    Also, I am having trouble getting my head around trying to allocate the info from the .txt file into an array of structs...any advice???
    Code:
    void load_master_data()
     {
    
    
        char** branchvalues;
        int i,y;
    
    
     FILE* branchPtr = fopen("Struct.txt", "r");
        if (!branchPtr)
        {
            printf("*******************************");
            branchPtr = fopen("Struct.txt", "w");
            if(branchPtr !=NULL)
                printf("\nNew file %s was created", branchPtr);            
        }
            
        
        branchvalues = (char**)malloc(sizeof(struct Branch)* GROWBY);
        
    
    
        do
        {
        i = fscanf(branchPtr, "%s%f%d", tempbranch.BranchName, tempbranch.NumberOfFiles, tempbranch.DateCreated);
        if(i!=0)
        {
            printf("%s%f%d", tempbranch.BranchName, tempbranch.NumberOfFiles, tempbranch.DateCreated);
            y++;
        }
        }
        while (i!=0);
    
    
        
        
        fclose(branchPtr);
    
    
     }

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,035
    There are a number of problems here. Here are a few:
    1)where's tempbranch defined? it should be a local variable.
    2)You call malloc, but never free. Then you destroy the pointer holding the memory. That's a memory leak.
    3)If you want to write to a a file, you cannot open it with "r". If you want to rad from it you cannot open it with "w"
    4)i and y are poor names under the circumstances. Names should be descriptive.
    5)y is never read (except ++y).
    6)y is never initialized.
    7)Why is branch values a char **, not a Branch*?
    8)branchPtr is not a branch pointer, despite the name. That's confusing.
    9)reading into tempbranch is good, but you probably want to write the temp branch into your array of branches.
    10) You probably want to make branch values available after the function call
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtok & fscanf: reading from file
    By Ralph Cacho in forum C Programming
    Replies: 8
    Last Post: 06-04-2011, 04:02 AM
  2. Reading file using fscanf into array
    By Sn0wcra5h in forum C Programming
    Replies: 3
    Last Post: 03-11-2010, 09:16 AM
  3. reading int and string from file fscanf
    By awesmesk8er in forum C Programming
    Replies: 5
    Last Post: 02-12-2009, 03:01 PM
  4. Ini File Reading using fscanf()
    By Mr.Modem in forum C Programming
    Replies: 5
    Last Post: 08-13-2005, 12:58 PM
  5. Using fscanf for reading numerical data from text file
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-15-2002, 05:18 PM

Tags for this Thread


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