Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Printf() not displaying as it should. Post 303031597 by ignatius on Saturday 2nd of March 2019 05:22:42 PM
Old 03-02-2019
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:

Code:
        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:



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.
 

10 More Discussions You Might Find Interesting

1. Programming

printf and imaxdif_t

i was playing with maxint stuff when i found that i could not find a propper way to do a printf() auf a imaxdiv_t. since nobody seems to use it google found nothing. i tried to find a PRIxy code but no success. example: #include <stdio.h> #include <inttypes.h> int main() { ... (2 Replies)
Discussion started by: grumpf
2 Replies

2. Shell Programming and Scripting

the printf command

hello, Im at another part of the program i am writing. Where i think i'm going to need to use the printf command. If anyone can help me figure out the printf layout i would greatly appreicate it. thanks (4 Replies)
Discussion started by: bebop1111116
4 Replies

3. Programming

printf

What is the output of the following program considering an x86 based parameter passing sequence where stack grows towards lower memory addresses and that arguments are evaluated from right to left: int i=10; int f1() { static int i = 15; printf("f1:%d ", i); return i--; } main() {... (2 Replies)
Discussion started by: arunviswanath
2 Replies

4. Shell Programming and Scripting

printf

How to print output in following format? A..................ok AA................ok AAA..............ok AAAAAA........ok "ok" one under one (4 Replies)
Discussion started by: mirusnet
4 Replies

5. UNIX for Dummies Questions & Answers

Displaying Number with printf

I am trying to display a number with commas printf "%d\n" 323232 printf "%d\n" 1234567 I want the output to be: 323,232 1,234,567 I tried to change %d to other formats and could find the solution. any idea? (7 Replies)
Discussion started by: ynixon
7 Replies

6. Shell Programming and Scripting

Displaying Number with printf

I am trying to display a number with commas printf "%d\n" 323232 printf "%d\n" 1234567 I want the output to be: 323,232 1,234,567 I tried to change %d to other formats and could find the solution. any idea? (7 Replies)
Discussion started by: ynixon
7 Replies

7. UNIX for Dummies Questions & Answers

Need help with printf

Hi, I have just completed my first script (:D) and now i just need to format it with printf. This is what I have: #!/bin/ksh TOTB=0 TOTF=0 TOTI=0 HOST=`hostname` echo " FSYSTEM BLKS FREE INUSE MOUNTEDON" df -m | grep -v ":"|grep -v Free|grep -v "/proc"| while read FSYSTEM... (2 Replies)
Discussion started by: compan023
2 Replies

8. UNIX for Dummies Questions & Answers

The meaning of %s in printf

I have this command like that has %s in it, I know %s calls a column, but I am not sure I understand which column (I mean for my case I can check the input file, but I want to know how is this %s used, how comes tha same symbo; gives different columns in one command line: {printf "grep %s... (22 Replies)
Discussion started by: cosmologist
22 Replies

9. UNIX for Dummies Questions & Answers

Using printf in bash

printf "%5.5\n" "1234567890" will print 12345 . How do I get it to print 67890 Essentially, I just want the last 5 characters rather than the first 5. (4 Replies)
Discussion started by: lavender
4 Replies

10. Shell Programming and Scripting

Not able to sort two fields and printf not displaying the correct values

Not able to sorting two fileds resolved printf issue 01-1000/9|JAN 01-0000/6|MAN 01-1010/2|JAN 01-1010/2|JAN 01-1010/2|JAN 01-1000/9|JAN 01-1000/9|JAN 01-1000/9|SAA 01-1000/9|SAA 01-0000/6|SAN 01-0000/6|SAN 1.sort -t'|' -k1,1n -k2,2 file (3 Replies)
Discussion started by: kalia4u
3 Replies
link(2) 							System Calls Manual							   link(2)

Name
       link - link to a file

Syntax
       link(name1, name2)
       char *name1, *name2;

Description
       A hard link to name1 is created; the link has the name name2.  The name1 must exist.

       With  hard links, both name1 and name2 must be in the same file system.	Unless the caller is the superuser, name1 must not be a directory.
       Both the old and the new share equal access and rights to the underlying object.

Return Values
       Upon successful completion, a value of 0 is returned.  Otherwise, a value of -1 is returned, and errno is set to indicate the error.

Diagnostics
       The system call fails and no link is created under the following conditions:

       [ENOTDIR]      A component of either path prefix is not a directory.

       [ENAMETOOLONG] A component of either pathname exceeded 255 characters, or the entire length of either pathname exceeded 1023 characters.

       [ENOENT]       A component of either path prefix does not exist.

       [ENOENT]       The file named by name1 does not exist.

       [ENOENT]       When name1 or name2 point to an empty string and the environment defined is POSIX or SYSTEM_FIVE.

       [EACCES]       A component of either path prefix denies search permission.

       [EACCES]       The requested link requires writing in a directory with a mode that denies write permission.

       [EEXIST]       The link named by name2 does exist.

       [EPERM]	      The file named by name1 is a directory, and the effective user ID is not that of superuser or  the  environment  defined	is
		      POSIX.

       [EXDEV]	      The link named by name2 and the file named by name1 are on different file systems.

       [EROFS]	      The requested link requires writing in a directory on a read-only file system.

       [EFAULT]       One of the pathnames specified is outside the process's allocated address space.

       [ELOOP]	      Too many symbolic links were encountered in translating one of the pathnames.

       [ENOSPC]       The  directory  in which the entry for the new link is being placed cannot be extended because there is no space left on the
		      file system containing the directory.

       [EDQUOT]       The directory in which the entry for the new link is being placed cannot be extended because the user's quota of disk blocks
		      on the file system containing the directory has been exhausted.

       [EIO]	      An I/O error occurred while reading from or writing to the file system to make the directory entry.

       [ESTALE]       The  file  handle  given	in the argument is invalid.  The file referred to by that file handle no longer exists or has been
		      revoked.

       [ETIMEDOUT]    A connect request or remote file operation failed because the connected party did not properly respond  after  a	period	of
		      time that is dependent on the communications protocol.

       [EMLINK]       The number of links to the file named by path1 would exceed {LINK_MAX}.

Environment
       In the POSIX environment, linking to directories is not allowed.

See Also
       symlink(2), unlink(2)

																	   link(2)
All times are GMT -4. The time now is 01:43 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy