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..

Recommended Answers

All 16 Replies

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.

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..

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...:cool:

#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
FILE *fp;
char data[50], *sd, p[50];
cout<< "Pls enter the string";
cin>> p;
fp=fopen("q.csv", "r");
if(fp==NULL)
{
perror("Problem encountered");
}
else
{
fgets(data, 50, fp);
sd=strstr(data,p);
if(sd!=NULL)
{
puts("file found");
}
else
{
puts("file not found");
}
puts(data);
}
return 0;
}

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.

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.

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.

:confused: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...

:confused: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...

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.

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..

:eek:I still cant understand. can you sate a sample pls? thanx..

Hmmmm, looks like you're tryin to run and you can't walk yet...

i am just a beginner and using c++ just 3 days ago. i hope you understand.. thanks..

Yep, I understand. As Sir Dragon said, this task seems to be beyond your capabilities. You don't seem to have the basics of looking at a string and figuring out what's in it yet. Back up to previous lessons and get to know strings.

Hmmmm, looks like you're tryin to run and you can't walk yet...


Yep, I understand. As Sir Dragon said, this task seems to be beyond your capabilities. You don't seem to have the basics of looking at a string and figuring out what's in it yet. Back up to previous lessons and get to know strings.

:sad:are there any function that trims out the string after the delimiter?

for example

1234, eyryrte
3132, fjifjisj12
1312, fasfaas

I want to erase the data on and after the comma each line, so that the remaining string would be 1234.. I want also to save that in a temporary file for later use..

help me pls.. i cant work it out....

:sad:are there any function that trims out the string after the delimiter?

Yes, there is/are always function(s) that do what you want..
RTFM is what others are trying to tell you very politely.. here is what I found for you on google.. Methods of interest for you would be, find_first_of(), substr()...

Yes, there is/are always function(s) that do what you want..
RTFM is what others are trying to tell you very politely.. here is what I found for you on google.. Methods of interest for you would be, find_first_of(), substr()...

i have one more problem..

i want to get the string to certain line in a csv file for comparison purposes.. here's the example:

213,wqeeq,213123,qweq
12,33123,31231,qerq
rdqa,313,442,df
34,dq,423,dfa

i want to get only the 3rd line and discard the other.
what should be the syntax im going to use?

pls help me.. thanx

if you want to read the 3d line, or any other line, in a file, just count the lines as they are being read and stop when you get to the line you want. You have to read text files sequentually -- from start to finish. If you don't know how to read a file then search the code cnippets for examples because I know they exist there.

Search the web to find how to read contents of a file line-by-line using a loop.
Once you have that in place, inside the loop, once you've read one line, split the required information using functions given in previous posts.
Using this information figure out if you this is the line you want or not. If this is NOT the line just go ahead read the next line and repeat process. If this is THE line do what you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.