Help with using a C program to display a text file a certain way.

This is a discussion on Help with using a C program to display a text file a certain way. within the C Programming forums, part of the General Programming Boards category; Hi, im tryin to make a C program, when displaying a text file, (as in myprogram.c < text.txt) to modify ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Help with using a C program to display a text file a certain way.

    Hi, im tryin to make a C program, when displaying a text file, (as in myprogram.c < text.txt) to modify the way it displays on the screen, in this case, for it to hide any blank line i have in the text file. eg.

    line 2
    and this is line 3
    4th line now

    this is the 6th line
    would turn into
    line 2
    and this is line 3
    4th line now
    this is the 6th line
    I have attempted to write a program for this, but i keep getting stuck.

    My current code is:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    
    int main( void )
    {
        char c;
        int count;
    
        while( (c = getchar()) != EOF )
    	if( c ) ( ! [A-Z] + "\n" || [0-9] +"\n" )
                putchar( iscntrl( c ) );
    	else
    	    putchar( c );
    
    }
    But at one stage i was able to remove the blank lines, unfortunately it removed all the "\n" parts of the code, thus putting everything in one big line.

    Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Maybe you should (re)read some basic tutorials on C. This is fairly basic stuff, and your code doesn't make sense even remotely.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    I thought that might be what would happen, well thanks anyway EVOEx, I got to the stage where i was making small modifications at a time to the code, thinking i was getting closer.
    Back to the books!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    hey Davoman,

    Welcome to the forum, and my advice is dump the idea of getting each char, one at a time, and using this instead:
    Code:
    char str[100] = {'\0'};
    
    while((fgets(str, sizeof(str), stdin)) != NULL) {
       if(str[1])
          printf("%s", str);
    }
    fgets() will put every line of text until the end of the file (when it return NULL), into the char buffer str. The if statement SHOULD stop printing of any lines that are empty (but I haven't tested it, so you're the guinea pig, buddy!

    str[] is a bit oversized, and that's good - those extra char's will never be printed. fgets() will include the newline into str, so each line will print out on a different row.

    Keep studying up, though. C takes some work, for sure.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Adak, thanks for your help mate, i only needed to add a else statement and i got exactly what i was trying to do!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  2. Can't display text from file
    By newbie543 in forum C Programming
    Replies: 1
    Last Post: 09-03-2005, 09:05 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 10:45 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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