How do I do it?
I have a text file in the form of:
18930 da_silvy
20389 karl_moore
20138 john
(for example)
how could i get it to return da_silvy when 18930 was the value?
Printable View
How do I do it?
I have a text file in the form of:
18930 da_silvy
20389 karl_moore
20138 john
(for example)
how could i get it to return da_silvy when 18930 was the value?
You do the same thing that if you were in VB...
Search each line, but it in a String then check for the space:
Here is the idea :
[NUMBER][Space][Name][NewLine]
So how would i do that?
Have you learned file handling?
It's really simple.Code:// C
#include <stdio.h>
void getName(int index, char *name)
{
FILE *f = fopen("thefile.dat", "rt");
while(!feof(f))
{
int i;
char buf[100];
fscanf("%i %s", &i, buf);
if(i == index)
{
strcpy(name, buf);
fclose(f);
return;
}
}
name[0] = '\0';
fclose(f);
}
// C++
#include <fstream>
#include <string>
using namespace std;
void getName(int index, string &name)
{
ifstream f("thefile.dat", ios::in | ios::text);
while(!f.eof())
{
int i;
string str;
f >> i >> str;
if(i == index)
{
name = str;
return;
}
}
name.clear();
}
Oh, I can't test that, so I can't guarantee that there are no errors - that's up to you.
If it's a few thousand records try reading the file into memory
use qsort to sort the array()
use bsearch() to lookup
This would be for a lookup intensive kind of application, like PLU for a scancode.
Otherwise for ten-100 records what CB gave you is fine.
Or for a one-time lookup. :D
In a lookup intensive app you don't keep hitting the file, you hit it once and get it over with.
Code:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int last=(-1);
void test(FILE *);
int cmp(void * void *);
int cmp1(void *, void *);
int find(const char *);
static char **list, tmp[256];
FILE *in;
int main(int argc, char *argv[]){
int i;
test(in=fopen("myfile.txt"));
while (!feof(in)){ // put text into array
memset(tmp,0x00,sizeof(tmp);
if(fgets(tmp,255,in)!=NULL){
list[++last]=calloc(sizeof(tmp),1);
strcpy(list[last],tmp);
}
qsort(list,(size_t) last,sizeof(tmp),cmp); // sort the array
fclose(in);
if( find("18960")!= NULL ) // bsearch lookup
printf("%s\n",list[find(]);
for(i=0;i<=last;i++) free(list[i]);
return 0;
}
void test(FILE *tst){
if(tst==NULL){
perror("Error opening inpout file");
exit(EXIT_FAILURE);
}
return;
}
int cmp(void *s, void *t){
return( strcmp( (char*)s,(char*)t);
}
char* find(const char *s){
return (char *) bsearch(s,list,sizeof(tmp),last,cmp1);
}
int cmp1(void *s,void *t){
return strncmp( ((char*) s, (char*)s, 6);
}
Yep right.
In C++ you might want to read the whole file into a map<int, string> container, with the numbers as keys and the names as values. Will REALLY speed up the lookup.
thanks for making me feel stupid :pQuote:
Originally posted by CornedBee
Have you learned file handling?
It's really simple.Code:// C
#include <stdio.h>
void getName(int index, char *name)
{
FILE *f = fopen("thefile.dat", "rt");
while(!feof(f))
{
int i;
char buf[100];
fscanf("%i %s", &i, buf);
if(i == index)
{
strcpy(name, buf);
fclose(f);
return;
}
}
name[0] = '\0';
fclose(f);
}
// C++
#include <fstream>
#include <string>
using namespace std;
void getName(int index, string &name)
{
ifstream f("thefile.dat", ios::in | ios::text);
while(!f.eof())
{
int i;
string str;
f >> i >> str;
if(i == index)
{
name = str;
return;
}
}
name.clear();
}
i haven't learned C++ before and wrote some different code that didn't seem to work :rolleyes:
thanks for your help guys ;)
Maybe if I got nothing better to do I'll code my second idea...