File I/O

This is a discussion on File I/O within the C Programming forums, part of the General Programming Boards category; Okay, here is some low hanging fruit. I have a text file with a million rows (1 record per row). ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39

    File I/O

    Okay, here is some low hanging fruit.

    I have a text file with a million rows (1 record per row). Each record is a string consisting of numbers, followed by an asterisk, followed by a single space. Each row is terminated with a carriage return+Line Feed. I need to create a second file that consists of all these records on one row.
    Code:
    Format: 
    1234* ,
    Here is what I have so far. The best I have been able to figure out is how to copy the contents of the source file into the new file.

    Thanks for the help!!
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    #define ARSIZE 16
    #define SRCNAME "C:\\ValidCust.txt"
    #define DSTNAME "C:\\newcust.txt"
    
    int main()
    {	
    	FILE *readfile; 
    	FILE *writefile;
    	char buffer[ARSIZE];
    	
    	errno = 0;
    	readfile = fopen( SRCNAME, "r" );
    	writefile = fopen( DSTNAME, "w" );
    
    	if( readfile == NULL || writefile == NULL )
    		fprintf(stderr,
    				"Open file error: %d, \"%s\"\n",
    				errno,
    				strerror( errno )
    				);
    				
    	while( fgets( buffer, sizeof( buffer ), readfile ) != NULL )
    	{
    		
    		fputs( buffer, writefile );
    	}
    
    	fclose( readfile );
    	fclose( writefile );
    
    	return EXIT_SUCCESS;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int c;
    while( (c = fgetc( infile )) != EOF )
    {
        if( c != '\n' && c != '\r' )
            fputc( c, outfile );
    }
    There.


    Quzah.
    Last edited by quzah; 05-22-2009 at 07:18 PM. Reason: missed a paren'
    Hope is the first step on the road to disappointment.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Um... pardon my brazen approach, but why does this require coding?
    Code:
    $ tr -d \\r\\n < your_input_file > your_output_file
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Because not everyone uses whatever OS / shell you just used?


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

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by quzah View Post
    Because not everyone uses whatever OS / shell you just used?
    Ok, so pop open your editor of choice. Edit --> Replace. The above was an example. This is a perfect example of the "right tool for the right job" principle. If you need to remove a specifc character, your tool of first choice should not be your compiler. If you lack the software that can do such a simple transformation, then you've got a bigger problem to remedy.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, text editors don't let you strip out newlines with just a search and replace. Because most of them use standard input boxes, which oddly enough, don't actually let you type in \n\r.


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

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    This is a programmer we are talking about. He should be working in something that has regular expression, and the vast majority of editors that can do regexs have the ability to rewrite newlines. Notepad++ (my editor of choice in Windows) can do this, and I would certainly hope that MSVS can too.

    I work with both Windows Vista & Windows XP - in my opinion, neither come with much useful software. That's why my first instinctive twitch (and, I should hope, of most users, but especially of programmers, too) is to put useful software onto it. A decent media player, an office suite, a decent IDE or plain-text editor, a hex editor, some useful tools (SysInternals is wonderful stuff), a picture editor that can work with modern day formats, the list goes on. Actually, now that I look, I have both bash and tr on my Vista partition -- granted, yes, I did put them there myself.

    I've done this at work too - I got asked "what changed between these two Excel spreadsheets"? Quick export to CSV, a bit of regexing to bring them in line, and (what should have been) a quick diff. (I could not for the life of me get diff to work on that machine. Had to seek assistance from a *nix machine...)

    A programmer who knows what tools are out there and uses them before re-inventing the wheel is a more useful programmer.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Cactus_Hugger View Post
    This is a programmer we are talking about.
    No, this is a would-be programmer. If he doesn't know how to write a program to remove newlines from a file, he's can't really call himself a programmer, can he? So it's not really a matter of having the right tool for the job. Because he probably doesn't have the needed tool.

    It's much faster for me to write the above code to strip newlines from a file, than it is to go hunt up some software that can do the job, and install it, and then have it do the job.

    So in this case, the best tool for the job may just be just a small program of a dozen lines.


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

  9. #9
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Thanks for the help mates!

  10. #10
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Quote Originally Posted by Cactus_Hugger View Post
    Um... pardon my brazen approach, but why does this require coding?
    Code:
    $ tr -d \\r\\n < your_input_file > your_output_file
    Because I'm a would-be programmer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-31-2004, 12:21 AM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 09:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM

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