Thread: File I/O Question

  1. #1
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Unhappy File I/O Question

    Greetings all,

    I am trying to figure out a way to open file1 read the data from it, then write that data to file2, but I need the data in a different format when written to file2 (this is why I don't think a straight file copy would work).

    Basically for ever piece of data in file one, I need it written to its own line in file 2 (So I figure I am looking for any whitespace that is between the data <i.e. new line, tab, or spaces>). I am really at a loss for how to scan the data, check it, then rewrite it to the new file in the other format.

    As always any advice, assistance, guidance, and help is greatly appreciated. A good swift boot in the right direction is also appreciated.

    Thanks much,
    DD
    "aut vincere aut mori"

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use the same "copy" program style code, but just use isspace() to determine if a character you read is white space, if so, don't write it out, write a newline instead. (and make sure you read in character by character using fgetc() ). Thats one way of doing it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Post Almost there... At least I think.

    Hammer, thanks for the guidance. For some reason I just did not think the copy would work like that.

    Anyway, I am almost there. I am getting my file to be read, and the data is being writen to the new file with a new line for each bit of space, But when I open the new file that the data was writen to, I am seeing some character and not the actual data that was in the read file. I am guessing I have something wrong with my fputc command in my code or maybe my fgetc. Anyone have any ideas? here is my current code.

    Code:
    #define W_S (sp == ' ' || sp == '\n' || sp == '\t')
    
    void fileIO (void)
    {	
    int c;
    int sp;
    int Status;
    FILE *fp1;
    FILE *fp2;
    
    if (!(fp1 = fopen("file1.dat", "r")))
    	{
    	printf("Error opening file for reading\a\n");
    	return;
    	} 
    	
    if (!(fp2 = fopen("file2.dat", "w")))
    	{
    	printf("Error opening file for writing\a\n");
    	return;
    	} 
    	
    while ((sp = fgetc(fp1)) !=EOF)
    	{
    	if (W_S)
    		{
    		fputc('\n', fp2);
    		}
    	else 	
    		fputc(c, fp2);
    	}
    
    fclose(fp1);
    Status = fclose (fp2);
    
    if (Status == EOF)
    	{
    	printf("Error closing write.dat file\a\n");
    	return;
    	}
    	
    
    return;
    
    }
    instead of isspace() I just defined whitespace (only because I am unfamiliar with isspace. I will be looking that one up, sounds like it would do the job much nicer for me. But basically there it is. Any ideas as to what I did that is causing my data to be turned into a strange character (looks like: Ì) instead of the actual data?

    Thanks for taking the time.
    DD
    "aut vincere aut mori"

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You're going to kick yourself for this one....

    Look here:
    Code:
    while ((sp = fgetc(fp1)) != EOF)
    ...
    fputc(c, fp2);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User DocDroopy's Avatar
    Join Date
    Jul 2002
    Posts
    32

    Talking Doh!!!!!

    Thanks again.

    I was doing it one way changed my mind and changed the variable. Can't believe I did not catch that. Somedays I am sure that the light I see ahead is a train rushing towards me.




    Thanks Much
    DD
    "aut vincere aut mori"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File i/o and ASCII question
    By muzihc in forum C Programming
    Replies: 13
    Last Post: 11-04-2008, 11:46 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  5. Another dumb question about file i/o
    By Cobras2 in forum C++ Programming
    Replies: 23
    Last Post: 03-14-2002, 04:15 PM