Thread: Program writing gibberish to a .dat file

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    Program writing gibberish to a .dat file

    Hello I am having a problem, I need my program to write data to a .dat file, then I need it to report/show my data, however I keep getting an error 'Access violation reading location', when I try to get it to show/report the data written? When I did some troubleshotting I noticed that gibberish is being written to my .dat file, is that why I am getting the error?

    Here is my code adding the data
    Code:
    void add()
    {
    	int inventory, catagory, quantity, choice, total_products = 0, ctyp, c, items, key;
    	double  cost, price, cateType[6], costs, category;
    	int n = 0, count = 0;
    	char discript[80]; 
    	int MININ, MAXIN, MINCAT, MAXCAT, MINQT, MAXQT; 
    	double MINCT, MAXCT, MINPR, MAXPR;
    	SGDATA rec;
    	FILE *fp, *sp;
    
    	fp = fopen("limits.dat", "rt");
    	if (fp == NULL)
    	{
    		printf("Error opening file\n");
    		perror("Error opening file");
    		getchar();
    	}
    	else
    	{
    		while (fscanf(fp, "%i%i%i%i%i%i%lf%lf%lf%lf", &MININ, &MAXIN, &MINCAT, &MAXCAT, &MINQT, &MAXQT, &MINCT, &MAXCT, &MINPR, &MAXPR) != EOF)
    		{
    
    			fclose(fp);
    		}
    
    		sp = fopen("limits1.dat", "ab");
    		if(sp == NULL)
    		{
    			printf("Error opening file");
    			return;
    		}
    
    		fseek(sp, 0L, SEEK_END);
    		c = (int)ftell(sp) / sizeof(rec);
    
    		
    		init_costs(cateType, 6);
    
    	do
    	{
    	
    		rec.inventory = validi(MININ, MAXIN, "Product number");
    		rec.catagory = validi(MINCAT, MAXCAT, "Product Type");
    		getstring(rec.discript);
    		strcase(rec.discript);
    		rec.quantity = validi(MINQT, MAXQT, "Quantity");
    		rec.cost = validf(MINCT, MAXCT, "Cost");
    		rec.price = validf(MINPR, MAXPR, "Price");
    
    		show(rec, rec.discript);
    		key = prompt("Are entries accurate?");
    		if(key == 'Y')
    		{
    			items = fwrite(&rec, sizeof(rec), 1, sp);
    			if(items == (int)NULL)message
    			("Error writing to file");
    			else c++;
    		}
    
    		fclose(sp);		
    printf("\nEnter category type (1-5);\n 1- Camping\n 2- Tennis\n 3- Golf\n 4- Snow sports\n 5- Water Sports ");
    		scanf("%d%*c", &ctyp);
    		printf("\nEnter Cost of Product Type: ");
    		scanf("%lf%*c", &costs);
    		
    
    		cateType[rec.catagory] += costs*rec.quantity;
    		show(rec, rec.discript);
    
    		choice = prompt("Continue");
    	}
    	while (choice == 'y' || choice == 'Y');
    	show_costs(cateType, 6);
    	getchar();
    	
    	}
    	
    }
    Here is my code to show/report my data
    Code:
    void report()
    {
    	int c, num = 0;
    	double total_profit;
    	SGDATA rec;
    	FILE *sp;
    	sp = fopen("limits1.dat", "rb");
    	if(sp == NULL)
    	{
    		printf("Error opening file\a"); 
    		return;
    	}
    
    	titles();
    	c = headings();
    	while(fread(&rec, sizeof(rec), 1, sp))
    	{
    		++num;
    		printf("-4i%8i%8s%8i%8lf%12lf%12lf\n", rec.inventory, rec.catagory, rec.discript, rec.quantity, rec.cost, rec.price, 
    			profit(rec.price, rec.cost, rec.quantity));
    
    		//total_profit += profit();
    		//printf("Total expected profit %5.1f", total_profit);
    	}
    
    	line('=', c-1);
    	printf("Done");
    	getchar();
    	fclose(sp);
    }
    I really appreciate any help.... SDI

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    while (fscanf(fp, "%i%i%i%i%i%i%lf%lf%lf%lf", &MININ, &MAXIN, &MINCAT, &MAXCAT, &MINQT, &MAXQT, &MINCT, &MAXCT, &MINPR, &MAXPR) != EOF)
    		{
    
    			fclose(fp);
    		}
    cough gack sputter. You read in a bunch of data -- not exactly correctly! Look at what fscanf returns, which is rarely EOF -- and if the read is successful, you close the file and then try to read some more data from the file!

    Your data may or may not be written correctly -- you are not writing it in a human-readable format, so just because you can't read it doesn't mean anything at all. That part looks right though, provided discript is declared as an array and not a pointer.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Being written when? If you write 2 records, then you display them back, do you get:

    garbage right away
    one record, then garbage
    two records then garbage

    Or something else entirely?
    Code:
    	while(fread(&rec, sizeof(rec), 1, sp))
    That really should be:
    Code:
    	while(fread(&rec, sizeof(rec), 1, sp) == 1 )
    I'm not really a big fan of variables in all caps:
    Code:
    	int MININ, MAXIN, MINCAT, MAXCAT, MINQT, MAXQT; 
    	double MINCT, MAXCT, MINPR, MAXPR;
    Typically all caps indicates a macro, though it isn't a language requirement.
    Code:
    	fp = fopen("limits.dat", "rt");
    	if (fp == NULL)
    	{
    		printf("Error opening file\n");
    		perror("Error opening file");
    		getchar();
    	}
    	else
    	{
    		while (fscanf(fp, "%i%i%i%i%i%i%lf%lf%lf%lf", &MININ, &MAXIN, &MINCAT, &MAXCAT, &MINQT, &MAXQT, &MINCT, &MAXCT, &MINPR, &MAXPR) != EOF)
    		{
    
    			fclose(fp);
    		}
    
    		sp = fopen("limits1.dat", "ab");
    Why are you opening it in text mode and then reading it, only to open it in binary mode when you write? Why do you have fclose in a loop?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    A few things...

    1) Why would you not have fscanf() write directly to the inventory record. Unnecessary intermediate variables accomplish nothing. If something isn't right, don't write the record.

    2) ALLCAPS variable names, while not wrong, are generally considered incorrect. By and large they are reserved for #define and constants.

    3) If you are opening the file for append, the file pointer is already correctly positioned at the end of the file. There's no reason to seek anywhere, unless you are doing in-place updates throughout the file.

    4) You may want to look at your validi() and validf() functions if there's garbage being written to your record that's most likely where it will come from.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Thanks for the replies! I am going to carefully read what ea of you has written, make some changes and report what I find.
    On a side note, my program in the beginning is reading from a file - limits.dat, and then my programming is writting to a completely different file - limits1.dat. Which is the file I want a report from. I am going to 'fine tooth comb it again'.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Quote Originally Posted by tabstop View Post
    Code:
    while (fscanf(fp, "%i%i%i%i%i%i%lf%lf%lf%lf", &MININ, &MAXIN, &MINCAT, &MAXCAT, &MINQT, &MAXQT, &MINCT, &MAXCT, &MINPR, &MAXPR) != EOF)
    		{
    
    			fclose(fp);
    		}
    cough gack sputter. You read in a bunch of data -- not exactly correctly! Look at what fscanf returns, which is rarely EOF -- and if the read is successful, you close the file and then try to read some more data from the file!

    Your data may or may not be written correctly -- you are not writing it in a human-readable format, so just because you can't read it doesn't mean anything at all. That part looks right though, provided discript is declared as an array and not a pointer.
    Thanks for the reply tabstop. The reason I closed the file is because it is a totally different file, then from the one I am writting to and attempting to report/show from. Also i am using an array for discript and not a pointer.

    Thanks again for the reply.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by wisdom30 View Post
    Thanks for the reply tabstop. The reason I closed the file is because it is a totally different file, then from the one I am writting to and attempting to report/show from.
    Well, yes. However, that doesn't stop you from going through that particular loop again and trying to read from fp (again) before reading from sp.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Didn't make very many changes... but here is my new code.

    Code:
    void add()
    {
    	int inventory, catagory, quantity, choice, total_products = 0, ctyp, c, items, key;
    	double  cost, price, cateType[6], costs, category;
    	int n = 0, count = 0;
    	char discript[80]; 
    	int MININ, MAXIN, MINCAT, MAXCAT, MINQT, MAXQT; 
    	double MINCT, MAXCT, MINPR, MAXPR;
    	SGDATA rec;
    	FILE *fp, *sp;
    
    	fp = fopen("limits.txt", "rt");
    	if (fp == NULL)
    	{
    		printf("Error opening file\n");
    		perror("Error opening file");
    		getchar();
    	}
    	else
    	{
    		while (fscanf(fp, "%i%i%i%i%i%i%lf%lf%lf%lf", &MININ, &MAXIN, &MINCAT, &MAXCAT, &MINQT, &MAXQT, &MINCT, &MAXCT, &MINPR, &MAXPR) != EOF)
    		{
    
    			fclose(fp);
    		}
    
    		sp = fopen("limits1.dat", "ab");
    		if(sp == NULL)
    		{
    			printf("Error opening file");
    			return;
    		}
    
    		fseek(sp, 0L, SEEK_END);
    		c = (int)ftell(sp) / sizeof(rec);
    
    		
    		init_costs(cateType, 6);
    
    	do
    	{
    	
    		rec.inventory = validi(MININ, MAXIN, "Product number");
    		rec.catagory = validi(MINCAT, MAXCAT, "Product Type");
    		getstring(rec.discript);
    		strcase(rec.discript);
    		rec.quantity = validi(MINQT, MAXQT, "Quantity");
    		rec.cost = validf(MINCT, MAXCT, "Cost");
    		rec.price = validf(MINPR, MAXPR, "Price");
    
    		show(rec, rec.discript);
    		key = prompt("Are entries accurate?");
    		if(key == 'Y')
    		{
    			items = fwrite(&rec, sizeof(rec), 1, sp);
    			if(items == (int)NULL)message
    			("Error writing to file");
    			else c++;
    		}
    
    		fclose(sp);
    
    		printf("\nEnter category type (1-5);\n 1- Camping\n 2- Tennis\n 3- Golf\n 4- Snow sports\n 5- Water Sports ");
    		scanf("%d%*c", &ctyp);
    		printf("\nEnter Cost of Product Type: ");
    		scanf("%lf%*c", &costs);
    		
    
    		cateType[rec.catagory] += costs*rec.quantity;
    		show(rec, rec.discript);
    
    		choice = prompt("Continue");
    	}
    	while (choice == 'y' || choice == 'Y');
    	show_costs(cateType, 6);
    	getchar();
    	
    	}
    	
    }
    My report code

    Code:
    void report()
    {
    	int c, num = 0;
    	double total_profit;
    	SGDATA rec;
    	FILE *sp;
    	sp = fopen("limits1.dat", "rb");
    	if(sp == NULL)
    	{
    		printf("Error opening file\a"); 
    		return;
    	}
    
    	titles();
    	c = headings();
    	while(fread(&rec, sizeof(rec), 1, sp) == 1)
    	{
    		++num;
    		printf("-4d%8d%8s%8d%8lf%12lf%12lf\n", rec.inventory, rec.catagory, rec.discript, rec.quantity, rec.cost, rec.price, 
    			profit(rec.price, rec.cost, rec.quantity));
    
    		//total_profit += profit();
    		//printf("Total expected profit %5.1f", total_profit);
    	}
    
    	line('=', c-1);
    	printf("Done");
    	getchar();
    	fclose(sp);
    }
    Here is an example of what my gibberish looks like... and I get this gibberish after every attempt to write to the file, it does successfully write characters.....as you can see green is my discription...
    Green 面面面面面面面面面面面面面面面面面面面面面面面面面 面面面面面面面面面面面面面面面面面面面面面面 @ @
    I have highlighted the area of my report function - this is where my program crashes and gives me the error....
    Any suggestions?
    Last edited by wisdom30; 05-15-2011 at 04:49 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    		while (fscanf(fp, "%i%i%i%i%i%i%lf%lf%lf%lf", &MININ, &MAXIN, &MINCAT, &MAXCAT, &MINQT, &MAXQT, &MINCT, &MAXCT, &MINPR, &MAXPR) != EOF)
    		{
    
    			fclose(fp);
    		}
    Good to see you aren't paying attention here.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    I am paying attention... seriously? Okay I am going to read what you wrote....

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is that what you get from your print statements or in your file? If the latter, that looks pretty much like you would expect. Looks like you have 5, 5, 5, Green, 18, and probably a floating point of some kind.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Quote Originally Posted by quzah View Post
    Code:
    		while (fscanf(fp, "%i%i%i%i%i%i%lf%lf%lf%lf", &MININ, &MAXIN, &MINCAT, &MAXCAT, &MINQT, &MAXQT, &MINCT, &MAXCT, &MINPR, &MAXPR) != EOF)
    		{
    
    			fclose(fp);
    		}
    Good to see you aren't paying attention here.


    Quzah.
    Is it wrong for me to close the file, if I don't need it anymore? Isn't it good form to close it after it has done its job, so stuff doesn't get written to it haphazardly? The file contains that min values and maximum values for a function. I have my program get it and input it into the MIN/MAX variables, then I close it. Should I not do that in the loop?

    Also you said the caps isn't language requirement, so I didn't change it. Do you want me to, even though it works fine the way it is?

    Thanks you Quzah!

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Quote Originally Posted by tabstop View Post
    Is that what you get from your print statements or in your file? If the latter, that looks pretty much like you would expect. Looks like you have 5, 5, 5, Green, 18, and probably a floating point of some kind.
    That is what I get in my file, and YES tabstop!!! That is what I entered! How do you see that and I don't? What am I doing wrong? THANKS FOR THE HELP!

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You probably don't have a nul character on the end of your string:
    Code:
    		getstring(rec.discript);
    		strcase(rec.discript);
    These probably just read until they run into a newline, and don't bother appending a \0. To fix this the lazy way, memset your rec before you start filling it to write it. Or make your string functions actually make strings.

    But that still isn't going to fix the part of the code we've already told you to fix multiple times now.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. If you unroll your while loop, this is what you are doing:
    Code:
    fscanf(fp, "%i%i%i%i%i%i%lf%lf%lf%lf", &MININ, &MAXIN, &MINCAT, &MAXCAT, &MINQT, &MAXQT, &MINCT, &MAXCT, &MINPR, &MAXPR);
    fclose(fp);
    fscanf(fp, "%i%i%i%i%i%i%lf%lf%lf%lf", &MININ, &MAXIN, &MINCAT, &MAXCAT, &MINQT, &MAXQT, &MINCT, &MAXCT, &MINPR, &MAXPR);
    Note that you are reading from a file after you have closed it. If you don't intend to do things over and over and over again, then don't put them in a loop.

    2. I was aided somewhat by the fact that you posted this in a web forum. You can see (well, I can see; I don't know how you have your browser set) that first character shows up in a box labeled "0005". If you are doing this in an editor, you're probably seeing a club (like in a deck of cards) assuming windows, or just a blank on a Mac. The I's are just the garbage in the other 74 characters of your 80-character string. The next character shows up here in a box labeled "0018" (double-headed up-and-down arrow in Windows). The floating point is represented by the @ signs; depending on what character the space actually is, who knows what the value is for sure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recving gibberish...
    By h_howee in forum Networking/Device Communication
    Replies: 4
    Last Post: 09-17-2007, 12:59 PM
  2. Replies: 1
    Last Post: 12-10-2005, 11:25 AM
  3. Why is it gibberish?
    By ober in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-10-2005, 01:24 PM
  4. Whats wrong with my program?(writing data into a file)
    By Ruflano in forum C++ Programming
    Replies: 1
    Last Post: 03-02-2002, 08:19 PM
  5. Help with a file writing program
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 01:56 AM