943,654 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2890
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 29th, 2007
0

csv problem

Expand Post »
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..
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
squinx22 is offline Offline
68 posts
since Mar 2007
Mar 29th, 2007
0

Re: csv problem

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Mar 29th, 2007
0

Re: csv problem

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..
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
squinx22 is offline Offline
68 posts
since Mar 2007
Mar 29th, 2007
0

Re: csv problem

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
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
squinx22 is offline Offline
68 posts
since Mar 2007
Mar 29th, 2007
0

Re: csv problem

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.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 29th, 2007
0

Re: csv problem

Click to Expand / Collapse  Quote originally posted by squinx22 ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Mar 29th, 2007
0

Re: csv problem

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...
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
squinx22 is offline Offline
68 posts
since Mar 2007
Mar 29th, 2007
0

Re: csv problem

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...
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
squinx22 is offline Offline
68 posts
since Mar 2007
Mar 30th, 2007
0

Re: csv problem

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.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Mar 30th, 2007
0

Re: csv problem

Click to Expand / Collapse  Quote originally posted by WaltP ...
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..
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
squinx22 is offline Offline
68 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ File Generator
Next Thread in C++ Forum Timeline: Function prototype?? Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC