开发者

Trouble reading .csv files in C++

I am working on a project where I intend on connecting to a database, grabbing a .csv file, reading it, manipulating the data and then returning it back to the database. Fairly simple and straight forward but I am still learning so if any one could point me in the right direction I would greatly appreciate it. Right now I have a simple program that is trying to read a .csv file and return the values to me printed on the console. I have been trying to find some good online resources for this but have came up short. Here is my code for what I have stumbled through so far.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{

int loop = 1;

while(loop = 1)
{

cout << "Welcome! " << endl;

ifstream myfile;

myfile.open ("..\\ Source\External\\ Sample.csv", ifstream::in);

The real path to this file is C:\Documents and Settings\RHatfield\My Documents\C++\Product Catalog Creator\Source\External\Sample.csv

while (myfile.good())
cout << (char) myfile.get();

myfile.close();
system("PAUSE");

return 0;
}


}

Now the issue is it does not print the values so I do not know that they are properly being captured. I have a feeling it is my file path but that's the only way I can find to write it without it throwing errors. I think it's something with the spaces in the file path but I can't seem to find another way to make it work. I am not looking for a handout and this is not homework or just regular work. I am trying to learn and having trouble teaching myself so if someone knows what开发者_开发百科 the issue is and can help me fix it or even point me to a relevant article online would be greatly appreciated.


Try the following code. I think the problem was you were making an assignment statement in the while condition statement.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
   cout << "Welcome! " << endl;

   ifstream myfile;

   myfile.open ("C:\\Documents and Settings\\RHatfield\\My Documents\\C++\\Product Catalog Creator\\Source\\External\\Sample.csv", ifstream::in);
   while (myfile.good())
      cout << (char) myfile.get();

   myfile.close();
   system("PAUSE");

   return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜