im trying to right align data that i have written to file with fprintf, but im not sure how i would do so in C. ive looked around and there are many solutions for C++ and C# but havent found one for to right align in regular C.

Recommended Answers

All 4 Replies

Tabs I guess would be an option.

Be more specific. Formatted text with fprintf is right aligned by default, so clearly you want to do something other than that.

well basically im writing to file with this

while(fgets(prime, 2000, fin)!=NULL) { 
    		root_value = atof(prime);
			
			fprintf(fout, "%.0f\t", root_value);
                     }

now i get the output that im looking for, however, it comes out like this
1
3
4
11
64
128
882

instead of this
                    1
                    3
                    4
                   11
                   64
                  128
                  882

Add a field width:

fprintf(fout, "%21.0f\t", root_value);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.