Like Tree1Likes

C Stock Sorting Program Issue

This is a discussion on C Stock Sorting Program Issue within the C Programming forums, part of the General Programming Boards category; Originally Posted by CommonTater Well, there's also this in your original message... Code: //Retrieve and print the binary file. void ...

  1. #16
    Registered User litebread's Avatar
    Join Date
    Dec 2011
    Location
    California
    Posts
    21
    Quote Originally Posted by CommonTater View Post
    Well, there's also this in your original message...
    Code:
    //Retrieve and print the binary file.
    void loadbin(struct Stock s[], int n)
    {
        FILE *f;
        f = fopen("e:\final.bin", "rb");
        fwrite(&s, sizeof(s[10]), n, f);
        fclose(f);
    }
    1) you need to double thos slashes ...
    2) you need to check if it actually opened or not...
    3) writing to a file you're trying to read ain't gonna work...
    4) size of s[10] isn't going to give you what youwant there...

    Code:
    void loadbin(struct Stock *s, int n)
    {
        FILE *f;
        f = fopen("e:\\final.bin", "rb");
        if (! f)
          { printf("Could not open the file");
             exit(1); }
        fread(s, sizeof(struct stock), n, f);
        fclose(f);
    }

    Ya, none of that worked .

    Here's the error exception:

    Code:
    Unhandled exception at 0x5c636af2 in cs36FINAL.exe: 0xC0000005: Access violation writing location 0x00000024.
    Current Snippet:

    Code:
    //Save the array of structures to a text file.void savetext(struct Stock s[], int n)
    {
    	FILE *f;
    	f = fopen("e:\\final.txt", "w"); 
    	int i;
    	for(i=0;i<n;i++)
    	{
    		fprintf(f,"%s\n",s[i].name);
    		fprintf(f,"%d  %f  %f  %f  %f  %f  %f", s[i].numShares, s[i].buyShare, s[i].currPrice, s[i].fees, s[i].initCost, s[i].currCost, s[i].profit);
    	}
    	fclose(f);
    	fflush(stdin);
    }
    //Retrieve and print the text file.
    void loadtext(struct Stock s[], int n)
    {
    	FILE *f;
    	f = fopen("e:\\final.txt", "r");
    	int i;
    	for(i=0;i<n;i++)
    	{
    		fgets(s[i].name, sizeof(s[i].name), f);
    		fscanf(f,"%d  %f  %f  %f  %f  %f  %f", s[i].numShares, s[i].buyShare, s[i].currPrice, s[i].fees, s[i].initCost, s[i].currCost, s[i].profit);
    	}
    	fclose(f);
    }
    //Save the array of structures to a binary file.
    void savebin(struct Stock s[], int n)
    {
    	FILE *f;
        f = fopen("e:\\final.bin", "wb");
        if (! f)
          { printf("Could not write to file");
             exit(1); }
        fread(s, sizeof(struct Stock), n, f);
        fclose(f);
    }
    //Retrieve and print the binary file.
    void loadbin(struct Stock s[], int n)
    {
        FILE *f;
        f = fopen("e:\\final.bin", "rb");
        if (! f)
          { printf("Could not open the file");
             exit(1); }
        fread(s, sizeof(struct Stock), n, f);
        fclose(f);
    }
    Debugger doesn't show any errors. THe error shows up on runtime and shows to this line:

    Code:
    fscanf(f,"%d  %f  %f  %f  %f  %f  %f", s[i].numShares, s[i].buyShare, s[i].currPrice, s[i].fees, s[i].initCost, s[i].currCost, s[i].profit);
    Uggghhhh Sorry guys. I'm new and stupid with farrrrrrr less experience than probably most of you. Could you skim through the code to see if there's something complicating this issue? I even checked the perms for the files. You see it will write them to disk, but won't read them, or bugs out when it attempts (By it i mean the IDE/Debugger). I honestly don't know whats causing it....

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to add the & before any non-string variable that you scan in (unless it's a pointer already).

    Code:
    	for(i=0;i<n;i++)
    	{
    		fgets(s[i].name, sizeof(s[i].name), f);
    		fscanf(f,"%d  %f  %f  %f  %f  %f  %f", &s[i].numShares, &s[i].buyShare, &s[i].currPrice, &s[i].fees, &s[i].initCost, &s[i].currCost, &s[i].profit);
    	}

  3. #18
    Registered User litebread's Avatar
    Join Date
    Dec 2011
    Location
    California
    Posts
    21
    Quote Originally Posted by Adak View Post
    You need to add the & before any non-string variable that you scan in (unless it's a pointer already).

    Code:
        for(i=0;i<n;i++)
        {
            fgets(s[i].name, sizeof(s[i].name), f);
            fscanf(f,"%d  %f  %f  %f  %f  %f  %f", &s[i].numShares, &s[i].buyShare, &s[i].currPrice, &s[i].fees, &s[i].initCost, &s[i].currCost, &s[i].profit);
        }
    I.LOVE.YOU. (No homo) Everything is now fine. Thank you so much!

    ........ing simple data types. I hate those pointers......

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I.LOVE.YOU. (No homo)
    Shucks, I was hoping for a good...never mind.

    You're welcome.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting issue
    By Ssalty in forum C++ Programming
    Replies: 1
    Last Post: 05-18-2011, 11:03 AM
  2. Stock Data
    By bskaer1 in forum C++ Programming
    Replies: 0
    Last Post: 10-17-2010, 12:59 AM
  3. compare issue while sorting
    By Micko in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2004, 10:34 AM
  4. Stock market
    By Liger86 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-20-2003, 06:12 PM
  5. Stock Taking program
    By C Babe in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2003, 08:40 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