Ignoring white spaces and putting all copied characters on 1 line.

This is a discussion on Ignoring white spaces and putting all copied characters on 1 line. within the C Programming forums, part of the General Programming Boards category; Hello. Currently my code is supposed to be copying the 21st character or the last character if it's less than ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    5

    Ignoring white spaces and putting all copied characters on 1 line.

    Hello. Currently my code is supposed to be copying the 21st character or the last character if it's less than 21 onto a new file. I need to ignore spaces, and it's not. In addition, I need it to place all these character copied on one line. Which I can't figure out what to do. Here's my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void){
    
        FILE* sp1;
        FILE* sp2;
        int curCh; // variable 1
        int preCh; // variable 2
        int countCh = 0; // variable 3;
        int whiteSpace = 0; // var4
        int closeStatus;
    
        printf("File copy has started\n");
        if (!(sp1 = fopen ("FILE1.DAT", "r"))){
            printf("Error opening FILES1.DAT for reading");
            return (1);
        }//if open
        if (!(sp2 = fopen ("FILES2.DAT", "w"))){
            printf("Error opening FILES2 for writing");
            return (2);
        }
    
        while ((curCh = fgetc(sp1)) != EOF){
            countCh++;
            if ( curCh == ' ')
                    whiteSpace = 0;
            if(curCh == '\n' ){
                if (countCh < 21 && whiteSpace != 0)
                    fputc (preCh, sp2);
                    fputc (' ', sp2);
                countCh = 0;
            }
    
            whiteSpace = 1;
            if ( countCh == 21 && whiteSpace != 0){
                fputc(curCh, sp2);
                fputc (' ', sp2);
    
            }
            preCh = curCh; 
        }        
        fputc ('\n', sp2);
        fclose(sp1);
        closeStatus = fclose(sp2);
        if (closeStatus == EOF)
        {
            printf("File close error.\a\n");
            return 201;
        } // if close error 
    
        printf("File successfully created\n");
        system ("pause");
        return 0;
    }]
    Last edited by code_bunny; 05-25-2012 at 06:45 PM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,330
    Another one of these guys?

    What is it with people and being deliberately unhelpful even to themselves?

    *shrug*

    I can see from looking at your code that you haven't included most any of the changes you've been told to make in any of the forums where you've posted this problem.

    Do try to help yourself. If you try to help yourself we will be happy to help you in turn. We aren't going to do it for you.

    For example, let's see you incorporate the changes that has already been suggested for exactly this code.

    Soma

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    5
    Thanks ..........

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-25-2012, 09:27 PM
  2. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  3. compare function ignoring leading white space
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-23-2008, 12:33 PM
  4. Ignoring New Line Characters
    By Spencer Wallace in forum C Programming
    Replies: 2
    Last Post: 03-27-2006, 12:23 AM
  5. Ignoring spaces...
    By alvifarooq in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2004, 11:30 PM

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