.csv file

Pages: 12
I have a Book1.csv file.
It contains list like this:
bones, origin ,deep or superficial,location,action
frontal,cartiledge,deep,cranial,flexes arms
Now, how can I search a bone, if user enters the action.

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
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>

using namespace std; 

int main ()
{
   using std::ifstream;   
   ifstream inStream;
  
   inStream.open("Book1.csv");
    
   if (inStream.fail())
   {
           cout<< "Input file opening failed.\n";
            exit (1);
    }
    
    inStream.close();
    
    
    system ("PAUSE");
    return 0;
}
Last edited on
Create a class 'bone' with the traits described in the list. Read the file in and create instances of the class 'bone' as elements in a vector of bones. give the class 'bone' a query method that returns true if passed the name of an action it can do.

Once you've done all that you'll able to write an algorithm that gets the name of an action and goes through the vector listing all bones that return true for the query method..
I think my file is not opening because it exits and if I take out the line exit (1), it outputs "Input file opening failed.". Do you know if the program I wrote should open my file?
Last edited on
¿Is the file there?
I would suggest to pass the filename (using completion) as a parameter to the program.
The file is on the desktop of the computer. How would I pass the filename (using completion) as a parameter to the program?
Last edited on
1
2
3
4
5
int main(int argc, char **argv){
   if(argc not_eq 2) return 1;
   std::ifstream input(argv[1]);
   //...
}


http://en.wikipedia.org/wiki/Command-line_completion
$ ./program.bin ~/desktop/book.csv
I can't specifically put in the desktop because I would also have to open in school. So, how can I do it so it would open on any computer which has that file somewhere?
.oi
$ ./program.bin /wherever/your/file/is/it/book.csv
Last edited on
Can't I do it without passing the filename (using completion) as a parameter to the program?

for your filename problem, try creating a string variable 'filename'. Then prompt the user to drag and drop the input file into the command window. Take the file location into the filename variable:

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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
string filename;

cout << "\nDrag input file here: ";
getline(cin, filename);

ifstream inStream;
inStream.open(filename.c_str());

if (inStream.fail())
   {
           cout<< "\n\nInput file opening failed.\n";
            exit (1);
    }


//........ ETC 

}



and go from there, doing something like what Cheraphy suggested :)
Last edited on
The file does not drag into the command window. Can I tell the user to enter the name of the file? The file will be exactly the same name and content on any computer I need it in. So, isn't there a way for the .csv file to be simply read, instead of having to drag it or asking user for it.
Last edited on
You can do it the way that you first tried, but I've found that while working on the project (I use Visual C++ 2010) if I put the file in the project folder (the one that contains the project source code and header files ) it will open it just fine. When I finish the program, I just put the input file in the same folder as the copied .exe release version of my program and it reads from it just fine.

so use inStream.open("Book1.csv");

but make sure that Book1.csv is in the folder with your source code instead of the desktop and see if that works.

I'm not sure if it's the same for every situation but that's what works for me so far.
Last edited on
The thing though is that I am not sure where my teacher will have the "Book1.csv" file saved and my program saved on the same folder. I thought reading a .csv would be simple as reading .txt file. Is it not?
Let me see if I understand.
You want to open a file named `Book1.csv'.
You don't have idea where that file is located
You don't want the user to write down the path (¿or do you?)

¿do you pretend to search the entire file system for your file?
Put some restrictions.
Move your Book1.csv file in your Executable's Working Directory?
Otherwise you will have to ask for a fully qualified path to the user.
Okay, so I put it in the same folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
using std::ifstream;
ifstream inStream;
inStream.open("Book1.csv");

if (inStream.fail())
   {
           cout<< "\n\nInput file opening failed.\n";           
    }
   
system ("PAUSE");
return 0; 

}


This is my code, but it still says "Input file opening failed."
Execute your program from that directory.
I have the .exe file in the same folder.
I bet you don't know what an "Working Directory" is.

Also I bet you're using the Visual Studio to run the program.

Tell me if I'm right, then if I am, try running the exe directly instead of running it from pressing F5 from the VS.
No, I'm using BloodShed Dev C++.
Pages: 12