Thread: Writing Data Randomly to a Random-Access File

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    Writing Data Randomly to a Random-Access File

    just trying to run and understand this example from the book. the code compiles and runs correctly but i get weird results in the file. the account number i enter appears as a symbol in file, and the double value i enter appears something like this in the file - ÌÌÌ£#¹ü‡t @. and records are placed randomly (i guess that's the result of random access file?). why is all of these?

    Code:
    struct clientData {	int account;
    	char firstName[10];
    	char lastName[15];
    	double balance;
    };
    
    
    int main() {
    
    
    	struct clientData client = {0, "", "", 0.0};
    	FILE *ptF;
    
    
    	if ((ptF = fopen("credit.txt", "rb+"))==NULL) {
    		printf("File could not be opened.\n");
    	} else {
    		printf("Enter account 1 to 100, 0 to end:\n? ");
    		scanf("%d", &client.account);
    		while (client.account != 0) {
    			printf("Enter first, last name, balance: ");
    			fscanf(stdin, "%s%s%lf", client.firstName,
    				client.lastName, &client.balance);
    			fseek(ptF, (client.account - 1)*sizeof(struct clientData), SEEK_SET);
    			fwrite(&client, sizeof(struct clientData), 1, ptF);
    			printf( "Enter account number\n? " );
    			scanf("%d", &client.account);
    		}
    		fclose(ptF);
    	}
    	getch();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How are you trying to view the file? You wrote the file in binary mode with the write() function so you can't view the file with a normal editor, you need to use something like a hex editor.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    okay i did that. is this how it should look like?

    Writing Data Randomly to a Random-Access File-capture-png

    i tried to read these data from file with another code and it printed the correct account number and balance, as i inputted (and not weird symbols), i understand that the weird symbols were because the file was being written in binary mode. and how can i view the file in a plain text format?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    how can i view the file in a plain text format?
    You can't. A binary file is not meant to be read like a text file. If you want a text format then you should open the file in "text" mode and use the formatted output methods like fprintf(). But remember you will give up random access, because a text file is considered a sequential file.



    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-10-2014, 09:53 AM
  2. Replies: 13
    Last Post: 07-29-2011, 10:32 PM
  3. reading then displaying some data from a random access file
    By kool_hamster in forum C Programming
    Replies: 5
    Last Post: 01-21-2010, 10:56 AM
  4. Reading data from a randomly accessed file
    By DLR in forum C Programming
    Replies: 2
    Last Post: 04-19-2006, 09:25 PM
  5. random file access
    By bob20 in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2002, 10:28 PM