1

I want to write a file in the format of table using c program. I am using fputs to write the file. My file looks like :

    [sl] [Name] [School] [add]
    1 ABC DEF Dav India
    2 XYZ LLL USA

But, I want output file to look like :

    [sl]    [name]     [school]    [add]
     1      ABC DEF     DAV        INDIA
     2      XYZ         LLL        USA

I want to know is there something like length specifier in fputs like in printf so that I can change format of the file.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Pratik Kumar
  • 67
  • 1
  • 7
  • If you know how many spaces you are going to add, why not just create a new string of size + no. spaces. Then use a pointer to the original to iterate down the original string writing X number of spaces for each found in the original as you copy from original to new? You could also split the original string into individual words and write them back out with fprintf including the additional format you need. – David C. Rankin Jun 27 '14 at 05:09
  • Can you show how your data is current held in your program. It is a bit ambiguous whether you have and existing string you need to write or if you have `s1` `name` `school` and `add` in different variable. Show us what you have. – David C. Rankin Jun 27 '14 at 05:17
  • I dont know exact length of each word. But I know maximum length of entries of each column. Say "name" wont have more than 50 letters . So just like printf if I give Left indentation & limit of 70 letters I will get a blank space of variable length & I can start entries of column "school" after 70 spaces . But how to do this with file – Pratik Kumar Jun 27 '14 at 05:19
  • I am writing in file like : fputs("1 ", fp); fputs ("ABC DEF ",fp); fputs ( "DAV ",fp); I am showing value as an example , actually it will be stored in a variable. – Pratik Kumar Jun 27 '14 at 05:20
  • If you can specify the individual parts of the string you want to write, then see Kyuubi's answer. fprintf is far more flexible for writing formatted data to a file than fputs. – David C. Rankin Jun 27 '14 at 05:22

2 Answers2

0

I think what you are looking for is fprintf. Find out more about fprintf here. It is used exactly the same way as printf, except it writes to a file.

EDIT

If your string is stored in different variables, then use fprintf as follows :

fprintf(fp,"%-15s %-15s %-15s %-15s", "string 1", "string 2", "string 3", "string 4");

The number 15 is the fixed length that the string will use (You can change this based on the maximum string length you have or some other preference). The minus sign is for left Justified string.

Community
  • 1
  • 1
Kyuubi
  • 1,228
  • 3
  • 18
  • 32
  • fprintf won't help, He has 1 string to write with X number of spaces in the original, he wants to increase the number of spaces that exist in the original string. – David C. Rankin Jun 27 '14 at 05:11
  • There is no way to have the formatting like a table otherwise, if he has a single string. Either he can slpit the string into multiple strings first and then print them using `fprintf`. – Kyuubi Jun 27 '14 at 05:13
  • You make a good point. I guess he will just have to post the relevant parts of his code showing the current storage of the data he wants to write out :-) – David C. Rankin Jun 27 '14 at 05:15
  • I am writing in file like : fputs("1 ", fp); fputs ("ABC DEF ",fp); fputs ( "DAV ",fp); I am showing value as an example , actually it will be stored in a variable. – Pratik Kumar Jun 27 '14 at 05:23
0

As Kyuubi specified, you will want to use fprintf instead of fputs it will cut down on the amount of code needed. The fprintf will write your data to a file pursuant to a format string. Since your data will be string data, you will write it out in pieces specifying %s in the format string for each piece. Take for example your ABC DEF words. You can use fprintf to write the string literals as follows:

fprintf (fp, "   %s    %s\n", "ABC", "DEF");

You can also specify the minimum field width for each word you write by specifying and integer between % and s, such as %10s to write '-------ABC' out with a minimum width of 10 characters. You can also place a - sign before the number to left align the string. For example %-10s to output 'ABC-------'. (the dashes are really spaces, just for illustration) The can help you separate the information you want into table form without writing out a varying number of spaces for each different fprintf call.

See 'man 3 fprintf` for all the options. There are literally 1000s of examples on the web to help.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85