Formatting a .csv file to print a table (C) [closed]
I have to print formatted table of csv file. I wonder if you know about any specific library or a tool that can help me with this - I just didn't find anything by googling.
this is the code and the code works fine, just have to print it like a formatted table. thanks!
void opportunity_table()
{
int i = 3;
char line[LINESIZE];
FILE* fp = fopen("opportunity_table.csv", "r");
if (!fp) {
printf("File failed to open!n");
exit(1);
}
while (fgets(line, LINESIZE, fp)) {
while (line[i] != 'n') {
if (line[i] == ',') {
printf("%s ", "");
}
else
printf("%c", line[i]);
i++;
}
i = 0;
puts(" ");
}
}
the input I get from running this code is messy and look really bad.
c
closed as off-topic by too honest for this site, Matthieu Brucher, AdrianHHH, Gert Arnold, Makyen Nov 25 '18 at 17:59
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – too honest for this site, Matthieu Brucher, AdrianHHH, Gert Arnold
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have to print formatted table of csv file. I wonder if you know about any specific library or a tool that can help me with this - I just didn't find anything by googling.
this is the code and the code works fine, just have to print it like a formatted table. thanks!
void opportunity_table()
{
int i = 3;
char line[LINESIZE];
FILE* fp = fopen("opportunity_table.csv", "r");
if (!fp) {
printf("File failed to open!n");
exit(1);
}
while (fgets(line, LINESIZE, fp)) {
while (line[i] != 'n') {
if (line[i] == ',') {
printf("%s ", "");
}
else
printf("%c", line[i]);
i++;
}
i = 0;
puts(" ");
}
}
the input I get from running this code is messy and look really bad.
c
closed as off-topic by too honest for this site, Matthieu Brucher, AdrianHHH, Gert Arnold, Makyen Nov 25 '18 at 17:59
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – too honest for this site, Matthieu Brucher, AdrianHHH, Gert Arnold
If this question can be reworded to fit the rules in the help center, please edit the question.
3
You should show some sample input data, and the actual and expected results — those are important parts of an MCVE (Minimal, Complete, and Verifiable example). Presumably, it is the output and not the input that yields the comment "the input I get from running this code is messy and looks really bad"?
– Jonathan Leffler
Nov 25 '18 at 15:53
add a comment |
I have to print formatted table of csv file. I wonder if you know about any specific library or a tool that can help me with this - I just didn't find anything by googling.
this is the code and the code works fine, just have to print it like a formatted table. thanks!
void opportunity_table()
{
int i = 3;
char line[LINESIZE];
FILE* fp = fopen("opportunity_table.csv", "r");
if (!fp) {
printf("File failed to open!n");
exit(1);
}
while (fgets(line, LINESIZE, fp)) {
while (line[i] != 'n') {
if (line[i] == ',') {
printf("%s ", "");
}
else
printf("%c", line[i]);
i++;
}
i = 0;
puts(" ");
}
}
the input I get from running this code is messy and look really bad.
c
I have to print formatted table of csv file. I wonder if you know about any specific library or a tool that can help me with this - I just didn't find anything by googling.
this is the code and the code works fine, just have to print it like a formatted table. thanks!
void opportunity_table()
{
int i = 3;
char line[LINESIZE];
FILE* fp = fopen("opportunity_table.csv", "r");
if (!fp) {
printf("File failed to open!n");
exit(1);
}
while (fgets(line, LINESIZE, fp)) {
while (line[i] != 'n') {
if (line[i] == ',') {
printf("%s ", "");
}
else
printf("%c", line[i]);
i++;
}
i = 0;
puts(" ");
}
}
the input I get from running this code is messy and look really bad.
c
c
asked Nov 25 '18 at 13:29
Benjamin YakobiBenjamin Yakobi
797
797
closed as off-topic by too honest for this site, Matthieu Brucher, AdrianHHH, Gert Arnold, Makyen Nov 25 '18 at 17:59
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – too honest for this site, Matthieu Brucher, AdrianHHH, Gert Arnold
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by too honest for this site, Matthieu Brucher, AdrianHHH, Gert Arnold, Makyen Nov 25 '18 at 17:59
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – too honest for this site, Matthieu Brucher, AdrianHHH, Gert Arnold
If this question can be reworded to fit the rules in the help center, please edit the question.
3
You should show some sample input data, and the actual and expected results — those are important parts of an MCVE (Minimal, Complete, and Verifiable example). Presumably, it is the output and not the input that yields the comment "the input I get from running this code is messy and looks really bad"?
– Jonathan Leffler
Nov 25 '18 at 15:53
add a comment |
3
You should show some sample input data, and the actual and expected results — those are important parts of an MCVE (Minimal, Complete, and Verifiable example). Presumably, it is the output and not the input that yields the comment "the input I get from running this code is messy and looks really bad"?
– Jonathan Leffler
Nov 25 '18 at 15:53
3
3
You should show some sample input data, and the actual and expected results — those are important parts of an MCVE (Minimal, Complete, and Verifiable example). Presumably, it is the output and not the input that yields the comment "the input I get from running this code is messy and looks really bad"?
– Jonathan Leffler
Nov 25 '18 at 15:53
You should show some sample input data, and the actual and expected results — those are important parts of an MCVE (Minimal, Complete, and Verifiable example). Presumably, it is the output and not the input that yields the comment "the input I get from running this code is messy and looks really bad"?
– Jonathan Leffler
Nov 25 '18 at 15:53
add a comment |
1 Answer
1
active
oldest
votes
Make use of the width and precision fields of the %s
specifier. The width field sets a width of at least the specified characters. The precision field will print up to the specified number of characters. Works as long as width is greater than precision.strpbrk
will give a pointer to the next character in the string or NULL.
The format string "%*.*s"
will right justify the printing. Use "%-*.*s"
to left justify.
#include <stdio.h>
#include <string.h>
#define WIDTH 7
int main( void) {
char csv = "a,b,cde,fghij,i,jhn";
char *item = csv;
char *comma = NULL;
while ( *item && ( comma = strpbrk ( item, ",n"))) {//pointer to each comma and the newline
printf ( "%*.*s", WIDTH, comma - item, item);
item = comma + 1;//skip the comma or newline
}
printf ( "n");
return 0;
}
If the width of the fields needs to vary, an array of widths could be used.
#include <stdio.h>
#include <string.h>
int main( void) {
char csv[4][50] = {
"a,b,cde,fghij,i,jhn",
"i,jk,lmno,pq,rst,uvwn",
"0,1,2,3456,78,9n",
"x,y,z,01,2345,6789n"
};
char *item = NULL;
char *comma = NULL;
int width = { 3, 4, 6, 7, 6, 5};
int field = 0;
for ( int loop = 0; loop < 4; ++loop) {
field = 0;
item = csv[loop];
while ( *item && ( comma = strpbrk ( item, ",n"))) {//pointer to each comma and the newline
printf ( "%*.*s", width[field], comma - item, item);
item = comma + 1;//skip the comma or newline
field++;
}
printf ( "n");
}
return 0;
}
This could be customized by reading the file twice. The number of fields and the maximum width could be determined in the first read. Read the file the second time and print using the calculated widths.
Thanks alot, exactly what I needed!
– Benjamin Yakobi
Nov 25 '18 at 20:18
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Make use of the width and precision fields of the %s
specifier. The width field sets a width of at least the specified characters. The precision field will print up to the specified number of characters. Works as long as width is greater than precision.strpbrk
will give a pointer to the next character in the string or NULL.
The format string "%*.*s"
will right justify the printing. Use "%-*.*s"
to left justify.
#include <stdio.h>
#include <string.h>
#define WIDTH 7
int main( void) {
char csv = "a,b,cde,fghij,i,jhn";
char *item = csv;
char *comma = NULL;
while ( *item && ( comma = strpbrk ( item, ",n"))) {//pointer to each comma and the newline
printf ( "%*.*s", WIDTH, comma - item, item);
item = comma + 1;//skip the comma or newline
}
printf ( "n");
return 0;
}
If the width of the fields needs to vary, an array of widths could be used.
#include <stdio.h>
#include <string.h>
int main( void) {
char csv[4][50] = {
"a,b,cde,fghij,i,jhn",
"i,jk,lmno,pq,rst,uvwn",
"0,1,2,3456,78,9n",
"x,y,z,01,2345,6789n"
};
char *item = NULL;
char *comma = NULL;
int width = { 3, 4, 6, 7, 6, 5};
int field = 0;
for ( int loop = 0; loop < 4; ++loop) {
field = 0;
item = csv[loop];
while ( *item && ( comma = strpbrk ( item, ",n"))) {//pointer to each comma and the newline
printf ( "%*.*s", width[field], comma - item, item);
item = comma + 1;//skip the comma or newline
field++;
}
printf ( "n");
}
return 0;
}
This could be customized by reading the file twice. The number of fields and the maximum width could be determined in the first read. Read the file the second time and print using the calculated widths.
Thanks alot, exactly what I needed!
– Benjamin Yakobi
Nov 25 '18 at 20:18
add a comment |
Make use of the width and precision fields of the %s
specifier. The width field sets a width of at least the specified characters. The precision field will print up to the specified number of characters. Works as long as width is greater than precision.strpbrk
will give a pointer to the next character in the string or NULL.
The format string "%*.*s"
will right justify the printing. Use "%-*.*s"
to left justify.
#include <stdio.h>
#include <string.h>
#define WIDTH 7
int main( void) {
char csv = "a,b,cde,fghij,i,jhn";
char *item = csv;
char *comma = NULL;
while ( *item && ( comma = strpbrk ( item, ",n"))) {//pointer to each comma and the newline
printf ( "%*.*s", WIDTH, comma - item, item);
item = comma + 1;//skip the comma or newline
}
printf ( "n");
return 0;
}
If the width of the fields needs to vary, an array of widths could be used.
#include <stdio.h>
#include <string.h>
int main( void) {
char csv[4][50] = {
"a,b,cde,fghij,i,jhn",
"i,jk,lmno,pq,rst,uvwn",
"0,1,2,3456,78,9n",
"x,y,z,01,2345,6789n"
};
char *item = NULL;
char *comma = NULL;
int width = { 3, 4, 6, 7, 6, 5};
int field = 0;
for ( int loop = 0; loop < 4; ++loop) {
field = 0;
item = csv[loop];
while ( *item && ( comma = strpbrk ( item, ",n"))) {//pointer to each comma and the newline
printf ( "%*.*s", width[field], comma - item, item);
item = comma + 1;//skip the comma or newline
field++;
}
printf ( "n");
}
return 0;
}
This could be customized by reading the file twice. The number of fields and the maximum width could be determined in the first read. Read the file the second time and print using the calculated widths.
Thanks alot, exactly what I needed!
– Benjamin Yakobi
Nov 25 '18 at 20:18
add a comment |
Make use of the width and precision fields of the %s
specifier. The width field sets a width of at least the specified characters. The precision field will print up to the specified number of characters. Works as long as width is greater than precision.strpbrk
will give a pointer to the next character in the string or NULL.
The format string "%*.*s"
will right justify the printing. Use "%-*.*s"
to left justify.
#include <stdio.h>
#include <string.h>
#define WIDTH 7
int main( void) {
char csv = "a,b,cde,fghij,i,jhn";
char *item = csv;
char *comma = NULL;
while ( *item && ( comma = strpbrk ( item, ",n"))) {//pointer to each comma and the newline
printf ( "%*.*s", WIDTH, comma - item, item);
item = comma + 1;//skip the comma or newline
}
printf ( "n");
return 0;
}
If the width of the fields needs to vary, an array of widths could be used.
#include <stdio.h>
#include <string.h>
int main( void) {
char csv[4][50] = {
"a,b,cde,fghij,i,jhn",
"i,jk,lmno,pq,rst,uvwn",
"0,1,2,3456,78,9n",
"x,y,z,01,2345,6789n"
};
char *item = NULL;
char *comma = NULL;
int width = { 3, 4, 6, 7, 6, 5};
int field = 0;
for ( int loop = 0; loop < 4; ++loop) {
field = 0;
item = csv[loop];
while ( *item && ( comma = strpbrk ( item, ",n"))) {//pointer to each comma and the newline
printf ( "%*.*s", width[field], comma - item, item);
item = comma + 1;//skip the comma or newline
field++;
}
printf ( "n");
}
return 0;
}
This could be customized by reading the file twice. The number of fields and the maximum width could be determined in the first read. Read the file the second time and print using the calculated widths.
Make use of the width and precision fields of the %s
specifier. The width field sets a width of at least the specified characters. The precision field will print up to the specified number of characters. Works as long as width is greater than precision.strpbrk
will give a pointer to the next character in the string or NULL.
The format string "%*.*s"
will right justify the printing. Use "%-*.*s"
to left justify.
#include <stdio.h>
#include <string.h>
#define WIDTH 7
int main( void) {
char csv = "a,b,cde,fghij,i,jhn";
char *item = csv;
char *comma = NULL;
while ( *item && ( comma = strpbrk ( item, ",n"))) {//pointer to each comma and the newline
printf ( "%*.*s", WIDTH, comma - item, item);
item = comma + 1;//skip the comma or newline
}
printf ( "n");
return 0;
}
If the width of the fields needs to vary, an array of widths could be used.
#include <stdio.h>
#include <string.h>
int main( void) {
char csv[4][50] = {
"a,b,cde,fghij,i,jhn",
"i,jk,lmno,pq,rst,uvwn",
"0,1,2,3456,78,9n",
"x,y,z,01,2345,6789n"
};
char *item = NULL;
char *comma = NULL;
int width = { 3, 4, 6, 7, 6, 5};
int field = 0;
for ( int loop = 0; loop < 4; ++loop) {
field = 0;
item = csv[loop];
while ( *item && ( comma = strpbrk ( item, ",n"))) {//pointer to each comma and the newline
printf ( "%*.*s", width[field], comma - item, item);
item = comma + 1;//skip the comma or newline
field++;
}
printf ( "n");
}
return 0;
}
This could be customized by reading the file twice. The number of fields and the maximum width could be determined in the first read. Read the file the second time and print using the calculated widths.
edited Nov 25 '18 at 14:53
answered Nov 25 '18 at 13:51
xingxing
1,254278
1,254278
Thanks alot, exactly what I needed!
– Benjamin Yakobi
Nov 25 '18 at 20:18
add a comment |
Thanks alot, exactly what I needed!
– Benjamin Yakobi
Nov 25 '18 at 20:18
Thanks alot, exactly what I needed!
– Benjamin Yakobi
Nov 25 '18 at 20:18
Thanks alot, exactly what I needed!
– Benjamin Yakobi
Nov 25 '18 at 20:18
add a comment |
3
You should show some sample input data, and the actual and expected results — those are important parts of an MCVE (Minimal, Complete, and Verifiable example). Presumably, it is the output and not the input that yields the comment "the input I get from running this code is messy and looks really bad"?
– Jonathan Leffler
Nov 25 '18 at 15:53