-1

I'm trying to write an Excel archive(input) into an empty archive. The aim is to write a new Excel without the dates, but when I try, the new archive is erasing some lines in the excel and not erasing the dates as I wanted to. Is it something with the code?

Input

1760   
02/20/18,11403.7    
02/19/18,11225.3   
02/18/18,10551.8   
02/17/18,11112.7
02/16/18,10233.9
02/15/18,10166.4
02/14/18,9494.63
02/13/18,8598.31

Actual Output

1760   
02/20/18   
11403.7    
02/19/18    
11225.3    
02/18/18    
10551.8    
02/17/18    
11112.7    
02/16/18    
10233.9    
02/15/18    
10166.4    
02/14/18    
9494.63    
02/13/18    
8598.31    

Expected Output

1760   
11403.7    
11225.3   
10551.8   
11112.7   
10233.9   
10166.4  
9494.63  
8598.31

This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int main(int argc,char **argv){
    FILE *btc;
    FILE *out;

        if((out = fopen("new.csv", "w")) == NULL ){
            printf("not found new\n");
            exit(1);
        }

        if((btc = fopen("BTC.csv", "r")) == NULL  ){
            printf("not found btc\n");
            exit(1);
        }                           
                    long int a;
                    char linha[256];
                    char *token = NULL;

                    while (!feof(btc))
                    {

                    fgets(linha, 256, btc);
                    token = strtok(linha, ",\n"); 

                            while ((token != NULL) && (!feof(btc)))
                            {
                            a++;
                            fprintf(out, "%s\n", token);
                            token = strtok(NULL, " \n");
                            }
                    }


    fclose(btc);
    fclose(out);


    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Andre
  • 115
  • 12
  • Those pictures should be reduced to the text that is in column A of the spreadsheet and shown as 'code' in the question. You probably only need 5-10 rows from each. – Jonathan Leffler Mar 11 '18 at 16:56
  • It would be better to open the input file first, before you create the output file. That way you don't leave empty files around if you can't open the input file. – Jonathan Leffler Mar 11 '18 at 16:57
  • 1
    See [`while (!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/) for a discussion of why your loop control is wrong. You need to test `fgets()` — forget `feof()`. Your input comes with multiple entries per input line; your output is splitting each line into multiple lines. It isn't clear if that's really your intention. You have `char num; fscanf(btc, "%c", &num);` — why do you ignore the first character in the file? (And your indentation is erratic — try to get it straight; it makes it easier for everyone, especially you.) – Jonathan Leffler Mar 11 '18 at 17:01
  • (Partial repeat of my first comment) It would be helpful if you showed your input — not the picture — and the expected and actual outputs, and gave a clearer explanation than "it is all wrong" for what is the difference between expected and actual output. This is an important part of creating an MCVE ([MCVE]). – Jonathan Leffler Mar 11 '18 at 17:04
  • See my answer — assume that if you are using `feof()` in a loop you are doing it wrong. A while ago, I searched through 30 years of my C code and found a couple of places (out of several thousand files) where I used `feof()`, and it was always used after a loop had terminated because of an I/O error, and I used `feof()` to distinguish between EOF and a real error (which could be tested with `ferror()`). All your indentation is erratic except the two lines declaring `btc` and `out`, the two `fclose()` calls and the `return 0;`. – Jonathan Leffler Mar 11 '18 at 22:57

1 Answers1

0

As far as I can see, you want to drop the date field, which is the first field. You need to arrange not to print the first token found by strtok() on each line — except the first where you should probably print 1760 by not scanning the 1 as a separate operation.

That leads to code like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    const char *name1 = "BTC.csv";
    const char *name2 = "new.csv";
    FILE *out;
    FILE *btc;

    if ((btc = fopen(name1, "r")) == NULL)
    {
        fprintf(stderr, "failed to open file %s for reading\n", name1);
        exit(1);
    }

    if ((out = fopen(name2, "w")) == NULL)
    {
        fprintf(stderr, "failed to open file %s for writing\n", name2);
        exit(1);
    }

    int lineno = 0;
    char linha[256];
    char *token = NULL;

    while (fgets(linha, sizeof(linha), btc) != 0)
    {
        if (++lineno == 1)
            fprintf(out, "%s", linha);
        else
        {
            token = strtok(linha, ",\n");
            if (token == NULL)
                break;
            while ((token = strtok(NULL, ",\n")) != 0)
                fprintf(out, "%s\n", token);
        }
    }

    fclose(btc);
    fclose(out);

    return 0;
}

Given input file BTC.csv:

1760
02/20/18,11403.7
02/19/18,11225.3
02/18/18,10551.8
02/17/18,11112.7
02/16/18,10233.9
02/15/18,10166.4
02/14/18,9494.63
02/13/18,8598.31

the program creates output file new.csv:

1760
11403.7
11225.3
10551.8
11112.7
10233.9
10166.4
9494.63
8598.31

This seems to correspond to what is wanted.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278