Thread: Horizontal output

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    44

    Horizontal output

    name.txt

    I
    k
    e
    S
    u
    e
    L
    e
    o


    I hope I have not overstayed my welcome but I refuse to take the easy way out, I want to do all I can with C for as long as possible. How can I get the above text to print to screen like below and save it to a file with a difference name?


    There may be better ways but I would like to know how this work because this is one of those time that I can’t even get lucky to get (%a,b, …) to do anything else except showing \n.


    One other thing; how would I put each character into char buffers like in the code below? Even if its not needed here based on the code, I just want to how it is done.


    new.txt

    Ike
    Sue
    Leo


    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define SEEK_SET 0
    #define SEEK_CUR 1
    #define SEEK_END 2
    
    
    int main() {
    
    
    char buf1[1]; char buf2[1]; char buf3[1]; char buf4[1];
    char cuf1[1]; char cuf2[1]; char cuf3[1]; char cuf4[1];
    char duf1[1]; char duf2[1]; char duf3[1]; char duf4[1];
    
    
    FILE *fptr, *fptw;
          fptr=fopen("name.txt","r");
          fptw=fopen("new.txt","w");
    
    
            fseek(fptr, 0, SEEK_SET);   int a = fgetc(fptr);
            fseek(fptr, 2, SEEK_SET);   int b = fgetc(fptr);
            fseek(fptr, 4, SEEK_SET);   int c = fgetc(fptr);
            fseek(fptr, 5, SEEK_SET);   int d = fgetc(fptr);     // new line
    
    
            fseek(fptr, 6, SEEK_SET);   int e = fgetc(fptr);
            fseek(fptr, 8, SEEK_SET);   int f = fgetc(fptr);
            fseek(fptr, 10, SEEK_SET);  int g = fgetc(fptr);
            fseek(fptr, 11, SEEK_SET);  int h = fgetc(fptr);     // new line
    
    
            fseek(fptr, 12, SEEK_SET);  int i = fgetc(fptr);
            fseek(fptr, 14, SEEK_SET);  int j = fgetc(fptr);
            fseek(fptr, 16, SEEK_SET);  int k = fgetc(fptr);
            fseek(fptr, 17, SEEK_SET);  int l = fgetc(fptr);
    
    
            printf(":  %c\n", a);
            printf(":  %c\n", b);
            printf(":  %c\n", c);
            printf(":: %c\n", d);
            printf(":  %c\n", e);
            printf(":  %c\n", f);
            printf(":  %c\n", g);
            printf(":: %c\n", h);
            printf(":  %c\n", i);
            printf(":  %c\n", j);
            printf(":  %c\n", k);
            printf(":: %c\n", l);
    
    
    //  putchar(fgetc(fh));
    //  fgets(c);
    //  fprintf(fptr,"d", a);
    //  fprintf(buf1,"d", a);
    
    
        fclose(fptr);
        fclose(fptw);
    
    
        return 0 ;
    }

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Simple:
    Code:
    /* horiz.c */
    #define _GNU_SOURCE
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main ( int argc, char *argv[] )
    {
      FILE *f;
      char s[8];  // 8 chars is sufficient (we expect just two!).
      int firstchar;
    
      // There is a filename in command line?
      if ( ! *++argv )
      {
        fprintf ( stderr, "Usage: %s <filename>\n", basename ( * ( argv - 1 ) ) );
        return EXIT_FAILURE;
      }
    
      // Tries to open the file.
      if ( ! ( f = fopen ( *argv, "r" ) ) )
      {
        perror ( "fopen" );
        return EXIT_FAILURE;
      }
    
      // While we can read a line...
      // FIXME: The line could be longer than 1 or 2 chars. In this case,
      // dynamic allocation using getline() would be better!
      firstchar = 1;
      while ( fgets( s, sizeof s, f ) )
      {
        // FIXME: We could check if there is an "empty" line here!
    
        if ( ! firstchar )
          // Uppercase chars start a newline.
          if ( isupper( *s ) )
            putchar( '\n' );
    
        // Print the char.
        putchar( *s );
        firstchar = 0;
      }
    
      // Last line
      putchar( '\n' );
    
      fclose ( f );
    
      return EXIT_SUCCESS;
    }
    Usage:

    $ cc -o horiz horiz.c
    $ ./horiz
    Usage: horiz <filename>
    $ cat test.txt
    I
    k
    e
    S
    u
    e
    L
    e
    o
    $ ./horiz test.txt
    Ike
    Sue
    Leo

    You can create the output file using:

    $ ./horiz test.txt > new.txt
    Last edited by flp1969; 07-18-2019 at 07:43 AM.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    I'd use redirection instead of messing with opening/closing files, since it's a simple text file. Also, since it's a stream that shouldn't be expected to handle direct user input, or messages to the user, you don't need to buffer every single line. Something like this:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main ()
    {
        int c;
    
        while ((c = fgetc(stdin)) != EOF) {
            if (isalpha(c)) {
                if (isupper(c)) {
                    putchar('\n');
                }
                putchar(c);
            }
        }
        putchar('\n');
    
        return 0;
    }
    and call it like this:
    Code:
    ./horiz < name.txt > new.txt
    Last edited by GReaper; 07-18-2019 at 08:24 AM.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    Thanks guys for these beautiful examples. Though they are not exactly what I’m after, they most certainly are going to be included in my program sooner than I think.


    For now, I use fseek, and it has already done exactly what I needed it to do to a certain point see code as is, at top of page. The only thing I’m really asking is how to write the result directly to a *file* in horizontal format.


    I’m hoping for an example with a loop and especially an example without the use of a loop. I prefer the extra bunch of coding lines.. it always tell me more then I break it down with loops and such, if needed or if I can.


    It been tough: my use of printf(": %c\n", a,b ..); and all else I tried only print the SEEK_SET data to the terminal screen vertically and that hasn’t been fun at all. It’s embarrassing to even ask because to print to a file the way I need can’t be that uncommon, I thought. Google and me don’t have a clue, but I got faith that someone here do. Either way, Ill never give up!

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by jc2020 View Post
    For now, I use fseek, and it has already done exactly what I needed it to do to a certain point see code as is, at top of page. The only thing I’m really asking is how to write the result directly to a *file* in horizontal format.
    Using fseek() to process text files is a problem if you get files created on Windows (each line terminates in 2 bytes: "\r\n". On Unix systems, just ONE byte ('\n') is used as terminator. Functions as fgets() will translate this for you automatically.

  6. #6
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    PS:

    I'd use redirection instead of messing with opening/closing files, since it's a simple text file.
    @GReaper, I usually open files this way. I picked up this type coding from a thread here at the cboard. I going to build around this tonight. IIRC it work for something when the standard way did not.
    Code:
    fptr = fopen("name.txt", "r");
           len = fread(arrayB, sizeof(char), MAX, fptr);
           arrayB[len] = '\0';
           fclose(fptr);
    .
    Also, since it's a stream that shouldn't be expected to handle direct user input, or messages to the user, you don't need to buffer every single line.
    @GReaper You correct, there is no user input, however my buffers are listed the way I prefer to do it .. byte-by-byte just like in assembly. I got the idea of how it works from an asm example that I saw while searching. However, there is no way I want deal with asm or C++ just to get one job done. Thanks @GReaper

    BTW: I use FBSD jails with no GUI. I don't care about portability. As long as it works for BSD and Linux that's all that matters.
    Last edited by jc2020; 07-18-2019 at 06:34 PM.

  7. #7
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    Functions as fgets() will translate this for you automatically.
    @flp1969 Do that mean I can print result to file? I tried fgets. Maybe I used it incorrectly. I’m going to read about fgets() now!

    Maybe something would include the function fseek() to make things clear.

  8. #8
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    fget() is doing something, I get no error but the character (I) did not enter the buffer. What am I missing here?
    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main() {
     
    FILE *fptr=fopen("name.txt","r");
     
    char buf1[1];                              // 0=I  2=k  4=e   5=NL
     
        fseek(fptr,0, SEEK_SET);               // New way
        fgets(buf1, 1, fptr);
        printf("buffer1 = %s \n", buf1);        // character I expected
     
        fclose(fptr);
     
        return 0; }
    Last edited by jc2020; 07-18-2019 at 08:55 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    A buffer of 1 character can only ever store an empty string because that 1 character will be the null character that terminates the string. If you want to store a string of length 1, you need a buffer of at least 2 characters.
    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

  10. #10
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    I tested this with 1, 2 and 20 character length. Same results for each:

    Something Might be Out of Order:

    Code:
        fseek(fptr,0, SEEK_SET);   fgets(buf1, 0, fptr);
    result =  []                                              strange char
     
     
        fseek(fptr,1, SEEK_SET);  fgets(buf1, 1, fptr);       0
    result =  
     
     
        fseek(fptr,2, SEEK_SET);  fgets(buf1, 2, fptr);       works!
    result =  k
     
        fseek(fptr,3, SEEK_SET);  fgets(buf1, 3, fptr);       0
    result =  
     
        fseek(fptr,4, SEEK_SET);  fgets(buf1, 4, fptr);       works!
    result =  e
     
        fseek(fptr,5, SEEK_SET);  fgets(buf1, 5, fptr);       works!
    result =  \n
    The good thing is it half way works! Now I got the chance to find what it takes to complete itself. My guest is to read the entire file into an array buffer with fread and go from there. Then I bet that @laserlight suggestion will come into play. It want be long

    Thanks @flp1969 for the deeper explanation of what fgets can do. Now that I got something to work with digging out the rest makes for good practice. Once I got it running properly, I’ll post the solution; hopefully by tomorrow evening.

    Thanks again everyone!

    Nothing is going to waste.

    EDIT: CORRECTION. @laserlight is right. It worked with 1 char lenght but a Segmentation fault was listed only under the buffered character [k] and for the new-line char. I think the fread function may solve that problem before saving to to a new file.
    Last edited by jc2020; 07-18-2019 at 11:40 PM.

  11. #11
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    fgets() prototype:
    Code:
    char *fgets(char *bufferptr, int size, FILE *stream);
    If you use size as 0 what is the size of the buffer? I believe your fgets() should be:
    Code:
    fgets(buf1, sizeof buf1, fptr);
    But, notice: You should test for error conditions after calling fseek() and fgets().

  12. #12
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    Got it!, now I can spend my time learning C the proper way. My project will be completed before this week is out. Thank you everyone. Thank you @Salem, for helping to get me started. Your code open my eyes even though I yet to show it. @laserlight, I IOU the world!


    Hope to find more examples here that are more involved.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define SEEK_SET 0
    #define SEEK_CUR 1
    #define SEEK_END 2
    
    
    int main() {
    
    
    FILE *fptr, *fptw;
          fptr=fopen("name.txt","r");
          fptw=fopen("new.txt","w");
    
    
    char cat1[5]; char cat2[5]; char cat3[5];
    
    
    char buf1[2]; char buf2[2]; char buf3[2]; char buf4[2];
    char bbf1[2]; char bbf2[2]; char bbf3[2]; char bbf4[2];
    char bbb1[2]; char bbb2[2]; char bbb3[2]; char bbb4[2];
    
    
    //  Get all needed characters from input file, one byte at a time. 
        fseek(fptr,0, SEEK_SET); fgets(buf1, 2, fptr);
        fseek(fptr,2, SEEK_SET); fgets(buf2, 2, fptr);
        fseek(fptr,4, SEEK_SET); fgets(buf3, 2, fptr);
        fseek(fptr,5, SEEK_SET); fgets(buf4, 2, fptr);
    
    
        fseek(fptr,6, SEEK_SET); fgets(bbf1, 2, fptr);
        fseek(fptr,8, SEEK_SET); fgets(bbf2, 2, fptr);
        fseek(fptr,10, SEEK_SET); fgets(bbf3, 2, fptr);
        fseek(fptr,11, SEEK_SET); fgets(bbf4, 2, fptr);
    
    
        fseek(fptr,12, SEEK_SET); fgets(bbb1, 2, fptr);
        fseek(fptr,14, SEEK_SET); fgets(bbb2, 2, fptr);
        fseek(fptr,16, SEEK_SET); fgets(bbb3, 2, fptr);
        fseek(fptr,17, SEEK_SET); fgets(bbb4, 2, fptr);
    
    
    //  conbind characters to create your custom strings.
    memcpy(cat1, buf1, 1);
    memcpy(cat1+1, buf2, 1);
    memcpy(cat1+2, buf3, 1);
    memcpy(cat1+3, buf4, 2);
    printf("moved %s \n", cat1);
    memcpy(cat2, bbf1, 1);
    memcpy(cat2+1, bbf2, 1);
    memcpy(cat2+2, bbf3, 1);
    memcpy(cat2+3, bbf4, 2);
    printf("moved %s \n", cat2);
    memcpy(cat3, bbb1, 1);
    memcpy(cat3+1, bbb2, 1);
    memcpy(cat3+2, bbb3, 1);
    memcpy(cat3+3, bbb4, 2);
    printf("moved %s \n", cat3);
    
    
    // Write each string to a file.
    fputs(cat1, fptw);
    fputs(cat2, fptw);
    fputs(cat3, fptw);
    
    
    // out put result to the terminal screen.
        printf("buffer1 = %s \n", buf1);
        printf("buffer2 = %s \n", buf2);
        printf("buffer3 = %s \n", buf3);
        printf("buffer4 = %s \n", buf4);
    
    
        printf("buffer1 = %s \n", bbf1);
        printf("buffer2 = %s \n", bbf2);
        printf("buffer3 = %s \n", bbf3);
        printf("buffer4 = %s \n", bbf4);
    
    
        printf("buffer1 = %s \n", bbb1);
        printf("buffer2 = %s \n", bbb2);
        printf("buffer3 = %s \n", bbb3);
        printf("buffer4 = %s \n", bbb4);
    
    
        fclose(fptr);
        fclose(fptw);
    
    
    //  All DONE.
    
    
       return 0; 
    }



    You the Man (?) @flp1969


    I'll be back with some serious C in my pocket.

  13. #13
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by jc2020 View Post
    You the Man (?) @flp1969
    Thanks... really "man". And looking like a trollface meme too:

    Horizontal output-eu_small-jpg

    That's me!

  14. #14
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    I use to think most programmers were men, but since trying to be one myself, I realize there are a great number of women programmers out here and you have to be careful not to use words like dude or man. I had to say hello. This was my first completed C program. While having fun I’ll was trying to be careful jic (?) Anyway, the only thing I remember about 1969 was the 1969 - 1976 Cutlass Supreme, 350 Rocket. 1976 is me! I never drove lass, I mean more or less.

  15. #15
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by jc2020 View Post
    Anyway, the only thing I remember about 1969 was the 1969 - 1976 Cutlass Supreme, 350 Rocket. 1976 is me! I never drove lass, I mean more or less.
    The story behind my username: I tried to register with a variation of my actual name (I don't like to "hide behind" an alias). My complete name is "Frederico Lamberti ........arra" (flp), and I was born in 1969... Since the forum didn't allow me to register as 'fred_........arra' or 'frederico_........arra', the successful registration was with 'flp1969'.

    I do realize, now, 'P I S S', from my name: 'P I S S A R R A' isn't allowed!
    Notice '....', above...
    Last edited by flp1969; 07-19-2019 at 02:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. horizontal scrollbar in listbox
    By umen242 in forum Windows Programming
    Replies: 4
    Last Post: 09-17-2010, 01:20 AM
  2. Horizontal ==> fscanf, Vertical ==> ???
    By AssistMe in forum C Programming
    Replies: 3
    Last Post: 02-28-2005, 03:08 AM
  3. List box horizontal bar
    By cfriend in forum Windows Programming
    Replies: 1
    Last Post: 09-12-2004, 03:52 PM
  4. no horizontal scroll bar?
    By scott27349 in forum C++ Programming
    Replies: 0
    Last Post: 03-16-2002, 10:41 PM
  5. Horizontal bar Graph?
    By robjules in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2001, 04:55 PM

Tags for this Thread