Hi everybody.

I'm trying to write the program that will read the file and delete blank spaces ONLY after the text.

here is the text :

serg@serg-PORTEGE-Z835:~$ cat sample.txt
                Now is the

here is the the same in hex:

serg@serg-PORTEGE-Z835:~$ od -x sample.txt
0000000 2020 2020 2020 2020 2020 2020 2020 2020
0000020 6f4e 2077 7369 7420 6568 2020 2020 2020

The program that I wrote gives me the result:

serg@serg-PORTEGE-Z835:~$ cat sample.out
                Now is th

As you can notice ther is no 'e' at the end,
the hex looks something like this:

0000000 2020 2020 2020 2020 2020 2020 2020 2020
0000020 6f4e 2077 7369 7420 0a68 000a
0000033

So my question is how to achive something like this:

0000000 0909 4e6f 7720 6973 2074 6865 0a09 0954

here is the code of my program:

#include<stdio.h>
#define SIZE 1000

int getLine(char *line, int lim);

int getLine(char *line, int lim) {
    int i;
    char c;

    for(i=0; i<lim-1 && (c=getchar()) !=EOF && c!='\n'; ++i) { 
        *(line+i) = c;
    }   
    
        line[++i]='\0';
    return i;
}

main() {
    int c,i;
    char line[SIZE];


    c=getLine(line, SIZE);
    
    for(i=c; i>0 &&  ((line[i]<='!') ||  (line[i]>='~')); i--){

        if (line[i]=='\n') {
            line[i]=0;
        }   
        else {
            line[i]=0;
            c--;
        }   
    /*  if ((line[i]<='!') && (line[i]>='~'))
            line[i]=0;
        else
            break;
    */
    }
   for( i=0; i<c ; i++) {
        printf("%c", line[i]);
    }

    printf("\n\n");
}

Get rid of all the hex and tell us exactly what you want... using ASCII.

So basically I want to kepp the text the way it is and get rid of all blank characters that are going after the text =)

I think he used the hex to indicate that there are spaces after the text

I think he used the hex to indicate that there are spaces after the text

Yes, exactly

Hi everybody.

I'm trying to write the program that will read the file and delete blank spaces ONLY after the text.

here is the text :

serg@serg-PORTEGE-Z835:~$ cat sample.txt
                Now is the

here is the the same in hex:

serg@serg-PORTEGE-Z835:~$ od -x sample.txt
0000000 2020 2020 2020 2020 2020 2020 2020 2020
0000020 6f4e 2077 7369 7420 6568 2020 2020 2020

The program that I wrote gives me the result:

serg@serg-PORTEGE-Z835:~$ cat sample.out
                Now is th

As you can notice ther is no 'e' at the end,
the hex looks something like this:

0000000 2020 2020 2020 2020 2020 2020 2020 2020
0000020 6f4e 2077 7369 7420 0a68 000a
0000033

So my question is how to achive something like this:

0000000 0909 4e6f 7720 6973 2074 6865 0a09 0954

here is the code of my program:

#include<stdio.h>
#define SIZE 1000

int getLine(char *line, int lim);

int getLine(char *line, int lim) {
    int i;
    char c;
    /* I don't like this conditional */
    for(i=0; i<lim-1 && (c=getchar()) !=EOF && c!='\n'; ++i) { 
        *(line+i) = c;
    }   
    
        line[++i]='\0';
    return i;
}

main() {
    int c,i;
    char line[SIZE];


    c=getLine(line, SIZE);
    
    for(i=c; i>0 &&  ((line[i]<='!') ||  (line[i]>='~')); i--){

        if (line[i]=='\n') {
            line[i]=0;
        }   
        else {
            line[i]=0;
            c--;
        }   
    /*  if ((line[i]<='!') && (line[i]>='~'))
            line[i]=0;
        else
            break;
    */
    }
   for( i=0; i<c ; i++) {
        printf("%c", line[i]);
    }

    printf("\n\n");
}

Ok, perhaps this works.....................

Ok, perhaps this works.....................

Yes it is, but not the way I want.
The output is different

Yes it is, but not the way I want.
The output is different

It's pretty simple. You are not looping fully while printing. Make this change in line 40 - 43 (extra line added) and it will work.

for( i=0; i<c ; i++) {
    printf("%c", line[i]);
}
printf("%c", line[i]);
This article has been dead for over six months. Start a new discussion instead.