Printf() not displaying as it should.

Hi, I have some code. Everything works as it should, but, when I call view_all_contacts() to print the data, each line doesn't line up as it should. I get tab keys between each line.

The problem code is this:

        printf("\n\e[1;34mNAME   :  \e[0;37m%s\n",name1);
        printf("\e[1;34mMOB NO. :  \e[0;37m%s\n",mob);
 

Here is the rest of the code:

void add_contact();
void search_contact();
void delete_contact();
void view_all_contact();

void add_contact()
{
    FILE *fp;
    fp=fopen("contact.txt","a+");
    printf("\e[20;10H\e[1;34mName     : \e[0;37m" );
    char name[20];
    scanw("%s",name);
    printf("\e[21;10H\e[1;34mMob No.  : \e[0;37m" );
    char mob[20];
    scanw("%s",mob);
    fprintf(fp,"%s %s ",name,mob);
    fclose(fp);
}
void search_contact()
{
    FILE *fp;
    fp=fopen("contact.txt","r");
    printf("\e[20;10H\e[1;34mSearch   : \e[0;37m");
    char name[20];
    scanw("%s",name);
    char name1[20],mob[20];
    while(fscanf(fp,"%s %s",name1,mob)!=EOF)
    {
        if(strcmp(name,name1)==0)
        {
            printf("\e[20;10H\e[1;34mNAME     : \e[0;37m%s\n",name1);
            printf("\e[21;10H\e[1;34mMOB NO.  : \e[0;37m%s\n",mob);
        }
    }
    fclose(fp);
}
void delete_contact()
{
    FILE *fp,*fp1;
    fp=fopen("contact.txt","r+");
    fp1=fopen("temp.txt","w");
    printf("\e[20;10H\e[1;34mEnter Name : \e[0;37m");
    char name[20];
    scanw("%s",name);
    char name1[20],mob[20];
    while(fscanf(fp,"%s %s",name1,mob)!=EOF)
    {
        if(strcmp(name,name1)==0)
        {
            continue;
        }
        fprintf(fp1,"%s %s\n",name1,mob);
    }
    fclose(fp);
    fclose(fp1);
    fp=fopen("contact.txt","w");
    fp1=fopen("temp.txt","r");
    while(fscanf(fp1,"%s %s",name1,mob)!=EOF)
    {

        fprintf(fp,"%s %s\n",name1,mob);
    }
    fclose(fp);
    fclose(fp1);
    remove("temp.txt");
}
void view_all_contact()
{
    FILE *fp;
    fp=fopen("contact.txt","r");
    char name1[20],mob[20];
    while(fscanf(fp,"%s %s",name1,mob)!=EOF)
    {
        printf("\n\e[1;34mNAME   :  \e[0;37m%s\n",name1);
        printf("\e[1;34mMOB NO. :  \e[0;37m%s\n",mob);
    }
    fclose(fp);
 }
  

Can someone tell me what is wrong with the code? Thanks for your time.

Without seeing the output you are getting and the output you are hoping to get, it is rather hard to guess at what, if anything, is wrong with the code you have shown us. My crystal ball just isn't working well enough today to be able to help with what you have provided.

It's basically like this:

           someinfohere
                              someinfohere
                                                 moreinfohere
                                                                   moreinfohere
                                                                                     etc..
                                                                                         etc...

--- Post updated at 10:50 PM ---

I'd like it to be like this:

someinfohere
someinfohere

moreinfohere
moreinfohere

etc..
etc..

What are your stty settings? It looks like the onlcr has been turned off in your stty output modes. You didn't show us any code playing with the terminal characteristics...

To what is standard output for your program directed?

Yes, I have multple stty settings in the source.

stty -echo raw
stty echo cooked -istrip

--- Post updated at 11:19 PM ---

Ok. I solved it. Thanks for your insight. I replaced the stty settins with what you suggested.

If you're going to go into stty raw mode and then try to get back to where you were before by using other stty commands, it is usually better to save the old settings and restore them exactly as they were initially with something like:

saveterm=$(stty �'g)	# save terminal state
stty (new settings)	# set new state
... 			# do whatever you want in the new terminal state
stty $saveterm		# restore original terminal state

Note that this is one of those places where it is important that you not enclose the expansion of $saveterm in double-quotes.

Hoping that you will get back to where you started just by using modes like cooked or sane always have a good chance of missing something that was important to the user who set up his/her terminal session before your script "messed with it".