Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a very primitive c++ programmer. For your information, here are code segments to read the excel csv file.
C++
//
             FILE *f_ptr6;
             FILE *f_ptr7;
//
             std::vector<string> xcord(1100000);
             std::vector<string> ycord(1100000);
             std::vector<string> zcord(1100000);
             std::vector<string> dsty(1100000);
             std::vector<string> ucs(1100000);
//
             if((err=fopen_s(&f_ptr6,"dc_data.csv","r")) !=0 )
                 printf( "The file 'dc_data.csv' was not opened\n" );
             else
                printf( "The file 'dc_data.csv' was opened\n" );
//
             if((err=fopen_s(&f_ptr7,"dc_data_out.txt","w+")) !=0 )
                printf( "The file 'dc_data_out.txt' was not opened\n" );
             else
                printf( "The file 'dc_data_out.txt' was opened\n" );
//
             char  string1[91]="  ";
             char *next_token1=NULL;
             char seps1[] = " ,\t\n";

             int i=0;

             while(fgets(string1, sizeof(string1), f_ptr6)!=NULL && !feof(f_ptr6))
             {      
                    xcord[i] = strtok_s(string1, seps,&next_token);
                    ycord[i] = strtok_s(NULL, seps,&next_token);
                    zcord[i] = strtok_s(NULL, seps,&next_token);
                    dsty[i] = strtok_s(NULL, seps,&next_token);
                    ucs[i] = strtok_s(NULL, seps,&next_token);
                    i=i+1;
             }

             fclose(f_ptr6);
//
// just write out part of the file
//
             for (int j=0; j<500;j++)
             {      
                 fprintf (f_ptr7, "%s %s     %s     %s     %s\n", xcord[j], ycord[j], zcord[j], dsty[j], ucs[j]);                    
             }
             fclose( f_ptr7);
If you have time please have a look. The excel csv file has five columns and the first row is title for each column.

What I have tried:

I tried to run it, but it cannot write out the file.
Posted
Updated 18-Jun-19 14:33pm
v2
Comments
Richard MacCutchan 18-Jun-19 17:49pm    
Use the debugger to see what data is being read in, and what written out.
Rick York 18-Jun-19 18:42pm    
I am surprised this even compiles. I always use WL 4 and this definitely would not. When you pass a std::string to a variadic function you need to call its c_str() method.

Why do you have a vector of 1.1M strings declared when you expect 500 lines in your file?

Are you sure your .CSV file is well-formed? I ask because if you have an empty entry which is two adjacent commas (,,) then strtok will skip over them both and null them. You will receive nulls from the calls when it runs out of tokens. For this reason, I think strspn is a better function to use for this because it does not null the delimiters it finds. It returns an index to the first one. Given this index, you can null them yourself after you have examined them and this lets can correctly handle empty fields in case you have them in your file(s). If you are processing output from Excel then there is a good chance you will get them.

As Richard stated, your best bet is to use the debugger and see what is going on.

1 solution

Hint: Rather than loop on intput file and read line and store values, then loop to output file, you can merge both loops and avoid using vectors.
loop
  read 1 line from input file
  decode/store values to variables
  write 1 line to output file
end of loop

Do not load a file to vectors if you have no reasons other than because it is possible. It gets no advantage and use huge memory.
Quote:
I tried to run it, but it cannot write out the file.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
CPallini 19-Jun-19 8:10am    
5.
Patrice T 19-Jun-19 9:20am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900