WTF is going on here.

This is a discussion on WTF is going on here. within the C Programming forums, part of the General Programming Boards category; Ok.... I'm trying to get this program to work that is supposed to get ride of all unnecessary white space ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    46

    Angry WTF is going on here.

    Ok.... I'm trying to get this program to work that is supposed to get ride of all unnecessary white space in a text file. But, it's not working can someone point me in the right direction.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main(int argc, char *argv[])
    {
    FILE *in, *out;
    int ch;
    char name[80];
    int count = 0;

    if (argc < 2)
    {
    fprintf(stderr, "Usage: %s filename\n",argv[0]);
    exit(1);
    }

    if ((in = fopen(argv[1], "r")) == NULL)
    {
    fprintf(stderr, "I couldn't open the file \"%s\"\n",argv[1]);
    exit(2);
    }

    strcpy(name, argv[1]);
    strcat(name, ".fnl");

    if ((out = fopen(name, "w")) == NULL)
    {
    fprintf(stderr, "Can't create output fule.\n");
    exit(3);
    }

    while ((ch = getc(in)) != EOF)

    if (count++ != " " && ++(++count)) != " "))
    putc(ch, out);

    if (fclose(in) != 0 || fclose(out) != 0)
    fprintf(stderr, "Error in closing files\n");
    return 0;
    }
    Last edited by Intimd8r; 11-19-2001 at 05:08 PM.

  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    Try this, instead of using a counter you use two reader variables, one to read the actual variable and one to test the next one and see if it's valid for the operation. Each time you read the whisker variable(whisker means it's feeling forward ), you test it for a space, if it's not space then you test it for EOF, if that's false too then you've got a valid character. Don't forget to ungetc() the whisker variable after that so that ch can read everything in order and you don't miss a character.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[]){
    	FILE *in, *out;
    	int ch, chWhisker;
    	char name[80];
    
    	if (argc < 2){
    		fprintf(stderr, "Usage: %s filename\n",argv[0]);
    		exit(1);
    	}
    
    	if ((in = fopen(argv[1], "r")) == NULL){
    		fprintf(stderr, "I couldn't open the file \"%s\"\n",argv[1]);
    		exit(2);
    	}
    
    	strcpy(name, argv[1]);
    	strcat(name, ".fnl");
    
    	if ((out = fopen(name, "w")) == NULL){
    		fprintf(stderr, "Can't create output fule.\n");
    		exit(3);
    	}
    
    	while ((ch = getc(in)) != EOF){
    		if (ch != " " && (((chWhisker = getc(in)) != " ") != EOF)){
    			ungetc(chWhisker); 
    			putc(ch, out);
    		if (fclose(in) != NULL || fclose(out) != NULL)
    			fprintf(stderr, "Error in closing files\n");
    	}
    	return 0;
    }
    pointer = NULL

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > if (count++ != " " && (++(++count)) != " "))

    Good god that's just wrong. Ok, it may be valid code, but it's fugly.

    You cannot compare a single character with a string. 'count' is a single character for comparison sake. " " is a string containing a space and a null terminator.
    Code:
    while( (c=fgetc( inputFile )) != EOF )
    {
        if( c == ' ' )
        {
            while( c==' ' && c != EOF ) c = fgetc( inputFile );
            if( c != ' ' ) ungetc( c, inputFile );
        }
    }
    How about something like that?

    /** edit: you beat me to it! **/

    Quzah.
    Last edited by quzah; 11-19-2001 at 05:35 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    46
    How are you calling the input file. I thought you always had to read in an array of characters work with that array then get a new one.

  5. #5
    Unregistered
    Guest
    > How are you calling the input file. I thought you always had to
    > read in an array of characters work with that array then get a
    > new one.

    Nah. Just use 'fgetc', and read a character at a time.

    FILE *inputFile = fopen("myfile.txt", "r" );
    int c;
    while( (c=fgetc(inputFile) != EOF ) printf("%c", c );
    fclose( inputFile );

    That'll dump the entire contents of the file to the screen.

    Quzah.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    God damnit. This is twice today it's signed me out while typing.
    This should have an extra parenthesis:

    > while( (c=fgetc(inputFile) != EOF ) printf("%c", c );

    Should be:

    while( (c=fgetc(inputFile)) != EOF ) printf("%c", c );

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Neck bomb kills Pizza man. WTF?
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 09-03-2003, 08:40 PM
  2. no... wtf!!!?? ( Global class is 'undeclared' )
    By knave in forum C++ Programming
    Replies: 5
    Last Post: 05-10-2003, 06:41 PM
  3. wtf is wrong with msdn
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-29-2002, 06:59 PM
  4. Wtf??? 7/9*9 = 0???
    By Tyrael in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2001, 10:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21