New Line Problem in ANSI-C program 
Author Message
 New Line Problem in ANSI-C program

Dear all,

I have a problem about the captioned.

I have a text file.
TXT content:
AAAA
BBBB
CCCC

DDDD
EEEE

FFFF

I want to use my C program to trim the new line space. Then store it
into array, and print it out.
OUTPUT:
AAAA
BBBB
CCCC
DDDD
EEEE
FFFF

Does any one tell me how to do?? I cannot check the new line character
"\n".

Cheers,
Alan



Tue, 24 May 2005 15:24:53 GMT  
 New Line Problem in ANSI-C program

Quote:

> [wants to strip empty lines from text file]

> Does any one tell me how to do?? I cannot check the new line character
> "\n".

What do you mean you cannot "check" the new-line character?  This
is more or less meaningless.

Here is one way to strip empty lines (untested, not compiled):

#include <stdio.h>

int
main (void)
{
        int last = 0;

        for (;;) {
                int ch = getchar ();
                if (ch == EOF)
                        break;
                if (ch != '\n' || last != '\n') {
                        putchar (ch);
                        last = ch;
                }
        }

        return 0;

Quote:
}



Tue, 24 May 2005 15:30:33 GMT  
 New Line Problem in ANSI-C program

Quote:

> Dear all,

> I have a problem about the captioned.

> I have a text file.
> TXT content:
> AAAA
> BBBB
> CCCC

> DDDD
> EEEE

> FFFF

> I want to use my C program to trim the new line space. Then store it
> into array, and print it out.
> OUTPUT:
> AAAA
> BBBB
> CCCC
> DDDD
> EEEE
> FFFF

> Does any one tell me how to do?? I cannot check the new line character
> "\n".

Sure you can.  The following skips the array building, but it should help:

#include <stdio.h>

[...]
    {
      char buf[SOME_BIG_ENOUGH_SIZE];
      while (fgets(buf,sizeof buf,stdin))
        if (buf[0] != '\n') fputs(buf,stdout);
    }
[...]



Tue, 24 May 2005 21:14:39 GMT  
 New Line Problem in ANSI-C program

Quote:
> Does any one tell me how to do?? I cannot check the new line character
> "\n".

Post your attempts. We don't know what you already know.

--
-ed- emdel at noos.fr ~]=[o
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/manuals/reader.aspx
"Mal nommer les choses c'est ajouter du malheur au monde."
-- Albert Camus.



Wed, 25 May 2005 04:43:59 GMT  
 New Line Problem in ANSI-C program

... problem statement snipped ...

Quote:

>Does any one tell me how to do?? I cannot check the new line character
>"\n".

The new line character is '\n', not "\n".

Could that be the problem?

--



Wed, 25 May 2005 07:51:21 GMT  
 New Line Problem in ANSI-C program

Quote:



> .... problem statement snipped ...

>>Does any one tell me how to do?? I cannot check the new line character
>>"\n".

> The new line character is '\n', not "\n".

> Could that be the problem?

He could use strstr(buf,"\n") rather than strchr(buf,'\n'), so I doubt that
that is the problem.


Wed, 25 May 2005 15:21:54 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Problem #30 on http://cs.nmu.edu/programming/c/problems.htm

2. New ANSI C standard ? ANSI vs ISO/IEC

3. Form1.cs on new applications

4. To ANSI or not to ANSI (was: Re: Just a minor new twist on free())

5. New ANSI C (ANSI C 95?) suggestion

6. Newbie: separate big .cs file into small .cs files

7. strtok() and new line problem

8. Macro problem (new lines in macros?)

9. Problems with reading a new line / carriage return

10. New Line Problem

11. Problem with strtok() and new line

12. Problem with entering new lines in VC5

 

 
Powered by phpBB® Forum Software