Thread: Writing to a file.

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    12

    Writing to a file.

    Hi folks,

    Have been having some issues with fileIO today, I am trying to read from a file containing the alphabet backwards and then perform a merge sort to put it back into order and then writing it back to a different file.

    Everything is fine up until the write operation, that is the array from the merge contains the alphabet in the correct order.

    Code:
    #include "stdafx.h"
    #include "stdio.h"
    
    FILE *fp = 0;
    FILE *outfp = 0;
    
    unsigned int store = 0;
    unsigned char i = 0;
    unsigned int temp = 0;
    
    
    unsigned char alphabet[26];
    unsigned int input[26];
    unsigned int scratch[26];
    
    void merge_reccur(unsigned int *input, unsigned int left, unsigned int right, unsigned int *scratch);
    void merge_sort(unsigned int *input, const unsigned int size);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    	
    	//Note the double back slashes to stop escape chars being generated.
    	fp = fopen("C:\\Users\\Pete\\Desktop\\fileio.txt", "r+b");	//Read and write binary.
    
    
    	fread(alphabet, sizeof(char), sizeof(alphabet), fp);	//Read in the alphabet (backwards).
    
    	for(i = 0; i < 26; i++)
    		input[i] = alphabet[i];	//Copy into integer array
    
    	merge_sort(input, (sizeof(input)/sizeof(int)));	//Merge sort the array.
    
    	for(i = 0; i < 26; i ++)
    	 alphabet[i] = input[i];	//Copy sorted array back into char array.
    
    	for(i = 0; i < 26; i ++)
    		printf("%c", alphabet[i]);	//Print sorted results back to console.
    
    	fclose(fp);
    
    	//Write to a different text file.
    
    	outfp = fopen("C:\\Users\\Pete\\Desktop\\output.txt", "wb");	
    	
    	for(i = 0; i < 26; i++)
    		fprintf(outfp, "%c", alphabet[i]);
    
    	temp = fwrite(alphabet, 1, sizeof(alphabet), outfp);
    	printf("%d", temp);
    	fclose(outfp);
    
    	while(1);
    }
    I have tried to use both fprintf and fwrite but neither seem to be working, although the value returned from fwrite is 26 which would seem to indicate that the array is being written somewhere!

    It would be greatly appreciated if someone could shed anylight on this ? I am using a win32 console application template on win64, could that make a difference??

    Cheers,

    Pete.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    In explorer, do you see a 52 byte file called output.txt ?

    If you can see the file, but not the contents (when viewed with say notepad), then try using a Hex editor. It's a basic (but useful) tool to go poking around in all sorts of files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You haven't really told us what the problem is. Is the file "output.txt" being created at all? What is it's size? Can you open it in notepad? What does it contain?

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    12
    Sorry oogabooga, I will try to be more clear in future, the issue was that the file was not being created at all.
    I got so frustrated with the problem that I shut down my computer and did something else, I have since returned and run the same program and it is now working as expected, very strange!

    Thanks anyway!

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by Pheetuz View Post
    Sorry oogabooga, I will try to be more clear in future, the issue was that the file was not being created at all.
    I got so frustrated with the problem that I shut down my computer and did something else, I have since returned and run the same program and it is now working as expected, very strange!

    Thanks anyway!
    You should still find what was causing the initial problem and fix it (even if it only affects you sometimes), just for the sake of future debugging.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Pheetuz View Post
    Sorry oogabooga, I will try to be more clear in future, the issue was that the file was not being created at all.
    I got so frustrated with the problem that I shut down my computer and did something else, I have since returned and run the same program and it is now working as expected, very strange!

    Thanks anyway!
    Were you logged in as Pete when running the program the first time?

    Windows desktops are user specific so if you were on as admin or some other user it would have created the file but not on your current desktop...

    Also... when you open an I/O device such as a file you should *always* check if it opened correctly or not...
    Code:
    FILE *fp
    
    fp = fopen("MyFile.txt","r")
    if (! fp)
      { 
         printf("Could not open the file!");
         exit(1);
       }
    This is exactly because you cannot count on input and output devices to always respond as expected. Paths get mistyped, users don't enter valid paths, files get moved around... any number of things can and do go wrong.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You should get rid of all those globals. They can probably all go in main. Also, I don't think any of your variables really needs to be unsigned. And there's no sense opening the file you're reading from in read/write mode. Just use read mode (get rid of the + sign). As for using binary mode, I'm not sure whether that's correct here, but it probably doesn't matter if you're not reading/writing newlines.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
        fclose(outfp);
     
        while(1);
    }
    What's the purpose of that?
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing to file
    By renvaar in forum C Programming
    Replies: 3
    Last Post: 03-18-2010, 02:51 PM
  2. Replies: 16
    Last Post: 04-20-2009, 04:33 PM
  3. Replies: 3
    Last Post: 11-21-2006, 07:26 PM
  4. reading from file and writing in a nother file
    By undisputed007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2004, 02:17 PM
  5. Writing to file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 01-17-2002, 09:37 AM