0

I am very new to programming. I am trying to write a program in C++ that converts text file into excel file.

Actually what i want is following my text data:-

0020.49
    0020.38
0020.49
    0020.37
0020.50
    0020.38
0020.50
    0020.37 

But i want to save it column wise in excel file like that:-

0020.49     0020.38
0020.49     0020.37      
0020.50     0020.38   
0020.50     0020.37

I am using visual studio 2010. Please tell me how can i do this?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Uzair
  • 121
  • 1
  • 10

1 Answers1

2

CSV is comma separated text.

So, all you need is to put commas between your values.

#include <iostream>
#include <stdio.h>

#define n 5
#define m 3

using namespace std;

int main() {

    double values[n][m];
    // Assign values here

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            printf("%.2f;", values[i][j]);
        printf("\r\n");
    }

    return 0;
}

According to your N and M.

Of course, this example outputs to default output. If you want it to be written to file, you need to use

FILE * fp = fopen("output.txt", "w+");
fprintf(fp, ...);

File will then have the following format and will be enterpreted by CSV-compatible reader (in your case, Excel):

0020.49;0020.38
0020.49;0020.37
0020.50;0020.38
0020.50;0020.37

P.S. CSV columns can be separated by comma or semicolon. Read this issue about which to use: CSV with comma or semicolon?

Community
  • 1
  • 1
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101