14 Replies - 870 Views - Last Post: 27 May 2012 - 10:12 PM Rate Topic: -----

#1 code_bunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-May 12

Trying to ignore spaces and put characters on one line in new file.

Posted 25 May 2012 - 05:02 PM

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.

#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);
				countCh = 0;

		}

		whiteSpace = 1;
		if ( countCh == 21 && whiteSpace != 0){
			fputc(curCh , 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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to ignore spaces and put characters on one line in new file.

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,165
  • Joined: 05-May 12

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 25 May 2012 - 05:40 PM

You requirements seem a little hazy. You need the 21st character from an input file to get written into another file. If there are less than 21 characters the last character read should be written into to the other file.

If there are more than 21 characters in the input file what should happen?

Where should the other characters read be written out to? The console? Yet another file?

In your code, you seem to be looking at linefeed characters as well. Do you need to do anything special with them that is not listed in your requirements?

And lastly there is a difference between characters and bytes. Does it matter if your input file data is ASCII, Unicode, UTF-8, UTF-16, etc.
Was This Post Helpful? 0
  • +
  • -

#3 code_bunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-May 12

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 25 May 2012 - 05:55 PM

View PostSkydiver, on 25 May 2012 - 05:40 PM, said:

You requirements seem a little hazy. You need the 21st character from an input file to get written into another file. If there are less than 21 characters the last character read should be written into to the other file.

If there are more than 21 characters in the input file what should happen?

Where should the other characters read be written out to? The console? Yet another file?

In your code, you seem to be looking at linefeed characters as well. Do you need to do anything special with them that is not listed in your requirements?

And lastly there is a difference between characters and bytes. Does it matter if your input file data is ASCII, Unicode, UTF-8, UTF-16, etc.

If there's 21+ characters ... then I just copy the 21st character always, unless there's less then copy the last character. I don't understand what "linefeed characters" are, sorry. Really new to this....
This is exactly what I'm supposed to do:
Write a program that copies the 21st character of each line in a file to a new file. All extracted characters are to be on the same line. If a line in the input file has fewer than 21 characters, write the last character. If a line is blank-that is, if it consists of only white space-then copy nothing. At the end of file, write a newline to the new file and close it.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,165
  • Joined: 05-May 12

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 25 May 2012 - 06:09 PM

Okay, with that problem restatement, where does the "ignore spaces" from your original post come from? From my reading of the statement, the only time spaces are to be ignored is if the entire line "consists of only white space".

So if the input file was:

123456789012345678901234567890
a                        z
                          
ABCDEFGHIJKLMNOPQRSTUVWXYZ
shorty



(where line 3 is all spaces) the output would be:

1 Uy


This post has been edited by Skydiver: 25 May 2012 - 06:12 PM

Was This Post Helpful? 0
  • +
  • -

#5 code_bunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-May 12

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 25 May 2012 - 06:20 PM

View PostSkydiver, on 25 May 2012 - 06:09 PM, said:

Okay, with that problem restatement, where does the "ignore spaces" from your original post come from? From my reading of the statement, the only time spaces are to be ignored is if the entire line "consists of only white space".

So if the input file was:

123456789012345678901234567890
a                        z
                          
ABCDEFGHIJKLMNOPQRSTUVWXYZ
shorty



(where line 3 is all spaces) the output would be:

1 Uy



If that was your input file, your output file should be "1zUy" All, I need is to not count spaces as characters in output, and put them all one line.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,165
  • Joined: 05-May 12

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 25 May 2012 - 06:33 PM

Where in your problem statement does it say to not count the spaces?
Was This Post Helpful? 0
  • +
  • -

#7 code_bunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-May 12

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 25 May 2012 - 07:24 PM

I need to not count the spaces as characters when extracting the characters and putting them onto the new file.
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 25 May 2012 - 09:18 PM

Do you want to increment countCh before checking for a space? Thereby counting the space.

25	        countCh++;
26	 
27	        if(curCh == ' ')
28	            whiteSpace = 0;




Maybe only do something if curCh is not a space.

  while ((curCh = fgetc(sp1)) != EOF) {
    if(curCh != ' ') {
      countCh++; 

      // ...

    }
  }


Was This Post Helpful? 1
  • +
  • -

#9 code_bunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-May 12

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 26 May 2012 - 03:43 AM

Thank you! I've updated my code, except now I am having trouble copying the last character in a line with less than 21 characters!

#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;
}

Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 26 May 2012 - 06:21 AM

View Postcode_bunny, on 26 May 2012 - 06:43 AM, said:

Thank you! I've updated my code, except now I am having trouble copying the last character in a line with less than 21 characters!

        if (curCh != ' '){
            whiteSpace = 0;
            countCh++;
        }
        // this is where I'm having the problem
        if(curCh == '\n'){
            if (countCh < 21 && whiteSpace !=0 ){
}

Look at the lines above your comment. If curCh is '\n', then curCh isn't ' ', so you have set whiteSpace=0 on line 2 and therefore the if statement on line 7 fails.
Was This Post Helpful? 1
  • +
  • -

#11 code_bunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-May 12

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 26 May 2012 - 02:08 PM

Thank you! I am still having trouble extracting the last characters in a line with less than 21 characters, it's a ':'. Also, I cannot figure out how to get all the extracted characters on one line in my new file.
Updated 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; // variable 4 = false
	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++;
		}

		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;
}

Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 26 May 2012 - 06:56 PM

When the current character is a newline, the character count is irrelevant. If the character count was 21 it would be caught in the previous iteration of the loop.

The whitespace variable is/was used to flag whether the current character is a space character. You could replace it with current_character_is_space and see if it is needed.
Was This Post Helpful? 0
  • +
  • -

#13 code_bunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-May 12

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 26 May 2012 - 08:15 PM

When I set current character to space, it copies my characters that are the 21st characters all one line. However, It doesn't copy my less than 21 characters, which is a 'p' and a ':'. When I set it to how it is in my above code currently, character not space or white space == , then it copies the 'p' character, but not the ':' -- and on different lines. Why not the ':'? Is it because I'm using int? Making that variable a char doesn't seem to help things.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,165
  • Joined: 05-May 12

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 26 May 2012 - 08:49 PM

Step through your code with a debugger. Or if you don't have debugger, put in a lot of cout's or printf's to see what the values of your variables are. You will learn a lot more by seeing what the program is actually doing as opposed to speculating about what it is doing.
Was This Post Helpful? 0
  • +
  • -

#15 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: Trying to ignore spaces and put characters on one line in new file.

Posted 27 May 2012 - 10:12 PM

Seems you are trying to be too complicated.

  preCh = curCh = ' ';

  while ((curCh = fgetc(sp1)) != EOF)
  {
    // a character that's not a space
    if (curCh != ' ')
    {
      countCh++;

      // is it end of line
      if(curCh == '\n')
      {
        fputc (preCh, sp2);
        fputc (' ', sp2); }
         
        countCh = 0;
        curCh = ' ';
      }
      else
      {
        // character not at EOL
        if ( countCh == 21 )
        {
          fputc(curCh, sp2);
          fputc (' ', sp2);

          countCh = 0;
          curCh = ' ';
        }
      }
      
      preCh = curCh;
    }  // if (!space)
  }  // while		


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1