Thread: Not copying blank space to new file and putting all characters on one line

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

    Not copying blank space to new file and putting all characters on one line

    Hello, I am trying to write a program that copies the 21st character of each line, excluding spaces. And if the line has less than 21 character to just copy the last character. So far, I can do all of that, but I just need to exclude the spaces and put all my results on 1 line in my new file. I just can't figure out what I'm missing. This is what I have so far.
    whiteSpace is being used as true/false it is whiteSpace = 0; up top, by the way.
    Code:
    while ((curCh = fgetc(sp1)) != EOF){
            countCh++;
    
            if(curCh == ' ')
                whiteSpace = 0;
            
                    if(curCh == '\n' ){
                        if (countCh < 21 && whiteSpace != 0)
                        fputc (preCh, sp2);
    
                countCh = 0;
    
            }
            
            whiteSpace = 1;
            if ( countCh == 21 && whiteSpace != 0){
                fputc(curCh , sp2);
    Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    At the moment, your indentation is misleading, which is a Bad Thing. You need to indent your code properly, e.g.,
    Code:
    while ((curCh = fgetc(sp1)) != EOF) {
        countCh++;
    
        if (curCh == ' ')
            whiteSpace = 0;
    
        if (curCh == '\n' ) {
            if (countCh < 21 && whiteSpace != 0)
                fputc (preCh, sp2);
    
            countCh = 0;
    
        }
    
        whiteSpace = 1;
        if (countCh == 21 && whiteSpace != 0) {
            fputc(curCh, sp2);
    Furthermore, you should show the entire while loop, along with the declarations and initial values of variables involved.

    Quote Originally Posted by code_bunny
    And if the line has less than 21 character to just copy the last character.
    What if the line consists entirely of spaces?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    5
    Okay, that's the entire thing. If there's just a blank line then do nothing ... I just need it to ignore space characters ' ', and put all the characters one line.
    Code:
    while ((curCh = fgetc(sp1)) != EOF){
            countCh++;
    
            if(curCh == ' ')
                whiteSpace = 0;
    
            if(curCh == '\n' ){
                if (countCh < 21 && whiteSpace != 0)
                    fputc (preCh, sp2);
                    countCh = 0;
    
            }
    
            whiteSpace = 1;
            if ( countCh == 21 && whiteSpace != 0){
                fputc(curCh , sp2);
    
    
            }
            preCh = curCh;
        }

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by code_bunny View Post
    Okay, that's the entire thing. If there's just a blank line then do nothing ... I just need it to ignore space characters ' ', and put all the characters one line.
    Code:
    while ((curCh = fgetc(sp1)) != EOF){
            countCh++;
    You've said that you want to print the 21st character in the line excluding whitespace but right now you count every character in the line, regardless whether it is whitespace or not.

    Code:
            if(curCh == ' ')
                whiteSpace = 0;
    Why do you set whiteSpace to 0 when you come across a space?
    Usually 0 means false and 1 means true.

    Code:
            whiteSpace = 1;
            if ( countCh == 21 && whiteSpace != 0){
                fputc(curCh , sp2);
    Since you've set whiteSpace to 1 just before the if, checking that it is not 0 is rather pointless.

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    5
    Thank you so much! Now it skips the the white spaces, but I seem to be having trouble with lines that have less than 21 characters on it. ><

    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){
            
            if (curCh != ' '){
                whiteSpace = 0;
                countCh++;
            }
            // this is where I'm having the problem
            if(curCh == '\n'){
                if (countCh < 21 && whiteSpace !=0 ){
                    fputc (preCh, sp2);
                    fputc (' ', sp2); }
                countCh = 0; 
            }
    
            whiteSpace = 1;
            if ( countCh == 21 ){
                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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking for a Blank Line When Reading From a File
    By jeanermand in forum C Programming
    Replies: 6
    Last Post: 02-07-2012, 06:50 PM
  2. multiline string, endl, \n, blank line space
    By jackson6612 in forum C++ Programming
    Replies: 9
    Last Post: 04-20-2011, 08:50 AM
  3. Reading a file containing a blank line
    By maniac123 in forum C++ Programming
    Replies: 8
    Last Post: 01-27-2011, 11:42 AM
  4. Input from file no white space characters
    By Dan17 in forum C++ Programming
    Replies: 5
    Last Post: 05-09-2006, 08:28 AM
  5. Replies: 3
    Last Post: 04-27-2005, 11:50 AM

Tags for this Thread