csv problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 61
Reputation: squinx22 is an unknown quantity at this point 
Solved Threads: 0
squinx22 squinx22 is offline Offline
Junior Poster in Training

csv problem

 
0
  #1
Mar 29th, 2007
somebody help me pls...
my task is to search a value that matches to my csv file.

my csv file contents is like this:

0,"100","WA"
101,"201","BC"
202,"302","EC"

this should be the scenario.

enter value: 205
output: BC

pls include the codes.. i am just a beginner.. thanks in advance..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: csv problem

 
0
  #2
Mar 29th, 2007
In your example if you enter "205" you will get nothing because there are no lines that contain "205" But I understand what you mean anyway.

>>pls include the codes
Please first post your code to show us the effort you are making to solve the problem.

I would read the file one line at a time using fgets() function. Then you can use either strstr() to see if the line contains the search string, such as "205" and if it exists print the remainder of the line.

Code the problem one small step at a time to help from getting overwhelmed with the assignment. First, write the program that just opens the file. Get that to compile and work correctly. Then add to it the code needed to read each line using fgets(). Once that works you can continue with the reset of the assignment.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 61
Reputation: squinx22 is an unknown quantity at this point 
Solved Threads: 0
squinx22 squinx22 is offline Offline
Junior Poster in Training

Re: csv problem

 
0
  #3
Mar 29th, 2007
pls send me a sample program with syntax in c++ so that i can study the program.. pls help me.. i am just a beginner and using c++ just 3 days ago. i hope you understand.. thanks..
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 61
Reputation: squinx22 is an unknown quantity at this point 
Solved Threads: 0
squinx22 squinx22 is offline Offline
Junior Poster in Training

Re: csv problem

 
0
  #4
Mar 29th, 2007
Originally Posted by Ancient Dragon View Post
In your example if you enter "205" you will get nothing because there are no lines that contain "205" But I understand what you mean anyway.

>>pls include the codes
Please first post your code to show us the effort you are making to solve the problem.

I would read the file one line at a time using fgets() function. Then you can use either strstr() to see if the line contains the search string, such as "205" and if it exists print the remainder of the line.

Code the problem one small step at a time to help from getting overwhelmed with the assignment. First, write the program that just opens the file. Get that to compile and work correctly. Then add to it the code needed to read each line using fgets(). Once that works you can continue with the reset of the assignment.
:eek:this is the program ive made earlier.. but it seems insufficient. it only reads the first line.. how cud i read each and every line one at a time?? thanks in advance...
  1. #include <iostream>
  2. #include <string>
  3. #include <stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. FILE *fp;
  10. char data[50], *sd, p[50];
  11. cout<< "Pls enter the string";
  12. cin>> p;
  13. fp=fopen("q.csv", "r");
  14. if(fp==NULL)
  15. {
  16. perror("Problem encountered");
  17. }
  18. else
  19. {
  20. fgets(data, 50, fp);
  21. sd=strstr(data,p);
  22. if(sd!=NULL)
  23. {
  24. puts("file found");
  25. }
  26. else
  27. {
  28. puts("file not found");
  29. }
  30. puts(data);
  31. }
  32. return 0;
  33. }
Last edited by WaltP; Mar 29th, 2007 at 2:15 pm. Reason: Add CODE tags -- explained on the background of the post text box -- and FORMAT your code
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: csv problem

 
0
  #5
Mar 29th, 2007
A few comments:
1. You're never using "p".
2. you don't have a loop to read till the end of file. Lookup the code snippets, I'm sure somewhere you'll find some code that reads from a file.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: csv problem

 
0
  #6
Mar 29th, 2007
Originally Posted by squinx22 View Post
pls send me a sample program with syntax in c++ so that i can study the program.. pls help me.. i am just a beginner and using c++ just 3 days ago. i hope you understand.. thanks..
That's a pretty complex assignment for someone who has only been studying c language for three days. If you are learning on your own you should start at the beginning of the book you are reading (you ARE studying a book aren't you???) and not in the middle.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 61
Reputation: squinx22 is an unknown quantity at this point 
Solved Threads: 0
squinx22 squinx22 is offline Offline
Junior Poster in Training

Re: csv problem

 
0
  #7
Mar 29th, 2007
Originally Posted by Ancient Dragon View Post
That's a pretty complex assignment for someone who has only been studying c language for three days. If you are learning on your own you should start at the beginning of the book you are reading (you ARE studying a book aren't you???) and not in the middle.
How could I separate the strings separated by commas and consider it as one string?

for example, i have a csv file:

from to name nick
0, 100, "help", "HP"
101, 201, "son", "SB"
202, 302, "dot", "DT"

if i enter a value the display wud be the nick. I must consider from and to.
for example

i entered 109, the program will then go to the second line since the value entered is in between the values 101 and 201. the output should be DT.

thanx in advance...
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 61
Reputation: squinx22 is an unknown quantity at this point 
Solved Threads: 0
squinx22 squinx22 is offline Offline
Junior Poster in Training

Re: csv problem

 
0
  #8
Mar 29th, 2007
How could I separate the strings separated by commas and consider it as one string?

for example, i have a csv file:

from to name nick
0, 100, "help", "HP"
101, 201, "son", "SB"
202, 302, "dot", "DT"

if i enter a value the display wud be the nick. I must consider from and to.
for example

i entered 109, the program will then go to the second line since the value entered is in between the values 101 and 201. the output should be DT.

thanx in advance...
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: csv problem

 
0
  #9
Mar 30th, 2007
Look at the first character
If it's a digit, call a function that converts the next values up to the comma into a number. Return the next location (pointer or index) in the string just after the comma

If it's a ", call a function that moves everything up to the next " into a character buffer. Return the next location (pointer or index) in the string just after the comma

repeat the above two texts until you run out of characters for the line.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 61
Reputation: squinx22 is an unknown quantity at this point 
Solved Threads: 0
squinx22 squinx22 is offline Offline
Junior Poster in Training

Re: csv problem

 
0
  #10
Mar 30th, 2007
Originally Posted by WaltP View Post
Look at the first character
If it's a digit, call a function that converts the next values up to the comma into a number. Return the next location (pointer or index) in the string just after the comma

If it's a ", call a function that moves everything up to the next " into a character buffer. Return the next location (pointer or index) in the string just after the comma

repeat the above two texts until you run out of characters for the line.
:eek:I still cant understand. can you sate a sample pls? thanx..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC