I am trying to convert a text file to a CSV file.
here is the file I'm trying to convert:
this txt file is meant
to be converted in to
a csv file using the
c on visual studios which
is provided by microsoft corporation
For some reason the output is coming out as:
corporation,corporation,corporation,corporation,corporation,
corporation,corporation,corporation,corporation,corporation,
corporation,corporation,corporation,corporation,corporation,
..........
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#define n 5
#define m 5
using namespace std;
int main() {
FILE *file;
file = fopen("file.txt", "r");
if (file == NULL) {
printf("no file to open \n");
return 0;
}
else {
printf("file found \n\n");
int i = 0, j = 0;
char values[n][m]; //string of characters //%s
for (i = 0; i < n; i++) { //assigns values to an array
for (j = 0; j < m; j++) {
fscanf(file, "%s", values);
}
}
fclose(file);
for (i = 0; i < n; i++) { //displays converted data
for (j = 0; j < m; j++) {
printf("%s,", values);
}
printf("\r\n");
}
printf("\n");
printf("creating .csv file \n\n");
FILE *f;
f = fopen("newfile.csv", "w+");
for (i = 0; i < n; i++) { //puts converted data into the csv file
for (j = 0; j < m; j++) {
fprintf(f, "%s,", values);
}
fprintf(f, "\r\n");
}
fclose(f);
return 0;
}
}
|
There is a problem with assigning a word to the array. I can't seem to figure it out. help is appreciated.
output i want:
this,txt,file,is,meant
to,be,converted,in,to
a,csv,file,using,the
c,on,visual,studios,which
is,provided,by,microsoft,corporation