Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Newbie Q: a C function like 'paste' ??

Reply
Thread Tools

Newbie Q: a C function like 'paste' ??

 
 
Nicolao
Guest
Posts: n/a
 
      01-15-2005
Sorry to use an non-C word like 'paste' but it's the quickest way to
describe my problem. My knowledge of C is clearly incomplete, to say
the least; I'm struggling to find a way to do what I need, and am way
out of my depth.

I'm trying to write a program which retrieves data from a postgreSQL
database (e.g. I enter a word, and the program checks with the database
what part-of-speech it is). I've adapted a script I found and I can
retrieve the data fine, but I don't want to just display it, I want to
store it. That's where I hit the problem, and I don't know enough of C
to solve it, and experimenting with the 2 books I have hasn't given me
a solution.

Obviously the retrieved data (e.g. 'noun') is in a buffer somewhere,
and if this were Filemaker on my Mac I could simply issue a 'paste'
command into an appropriate field. But I can't find a successful way to
assign what's in the buffer to the variable 'part'.

I'll show you what I have, and mark where I reckon the missing move
should be. I'd really appreciate some advice here, and please forgive
the gaping holes in my knowledge - I sure must have missed something!

Incidentally, I'm doing this on a Debian box (woody on a powerpc)

Nic



/* WORDFETCH.C, a program to consult database 'wordpart' */
/* compile like this: gcc -o wordfetch -I/usr/include/postgresql
wordfetch.c -lpq */


#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"

int main()
{
char word[9]; /* holds word entered by user */
char part[12]; /* to hold copied part-of-speech from database */
char query_string[256]; /* holds constructed SQL query */
PGconn *conn; /* holds database connection */
PGresult *res; /* holds query result */
int i;

conn = PQconnectdb("dbname=wordpart"); /* connect to database */

if (PQstatus(conn) == CONNECTION_BAD) /* did connection fail? */
{
fprintf(stderr, "Connection to database failed.\n");
fprintf(stderr, "%s", PQerrorMessage(conn));
exit(1);
}

printf("Type a word: "); /* prompt user for word to be analysed */
scanf("%s", word);

sprintf(query_string, "SELECT part FROM list WHERE word = '%s'",
word); /* create SQL query string */

res = PQexec(conn, query_string); /* sends the query */

if (PQresultStatus(res) != PGRES_TUPLES_OK) /* did the query fail? */
{
fprintf(stderr, "SELECT query failed.\n");
PQclear(res);
PQfinish(conn);
exit(1);
}

for (i = 0; i < PQntuples(res); i++) /* loop through all
rows */
printf("result = %s\n", PQgetvalue(res, i, 0)); /* display the
value returned */


/* HERE IS WHERE I THINK THE ASSIGNMENT SHOULD BE; I want to 'paste'
into 'part' */


PQclear(res); /* free result */

PQfinish(conn); /* disconnect from database */

printf("%s = %s\n", word, part); /* a tester to check */

return 0;
}
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      01-15-2005
Nicolao <> scribbled the following:
> Sorry to use an non-C word like 'paste' but it's the quickest way to
> describe my problem. My knowledge of C is clearly incomplete, to say
> the least; I'm struggling to find a way to do what I need, and am way
> out of my depth.


> I'm trying to write a program which retrieves data from a postgreSQL
> database (e.g. I enter a word, and the program checks with the database
> what part-of-speech it is). I've adapted a script I found and I can
> retrieve the data fine, but I don't want to just display it, I want to
> store it. That's where I hit the problem, and I don't know enough of C
> to solve it, and experimenting with the 2 books I have hasn't given me
> a solution.


> Obviously the retrieved data (e.g. 'noun') is in a buffer somewhere,
> and if this were Filemaker on my Mac I could simply issue a 'paste'
> command into an appropriate field. But I can't find a successful way to
> assign what's in the buffer to the variable 'part'.


Looks like you want strcpy(). If that's not what you want, please try
to explain in more detail, using programming language jargon instead of
Mac desktop application jargon.

--
/-- Joona Palaste () ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I am lying."
- Anon
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      01-16-2005

"Nicolao" <> wrote
> I'm trying to write a program which retrieves data from a postgreSQL
> database (e.g. I enter a word, and the program checks with the database
> what part-of-speech it is). I've adapted a script I found and I can
> retrieve the data fine, but I don't want to just display it, I want to
> store it. >
>
> /* WORDFETCH.C, a program to consult database 'wordpart' */
> /* compile like this: gcc -o wordfetch -I/usr/include/postgresql
> wordfetch.c -lpq */
>
>
> #include <stdio.h>
> #include <stdlib.h>
> #include "libpq-fe.h"
>
> int main()
> {
> char word[9]; /* holds word entered by user */
> char part[12]; /* to hold copied part-of-speech from database */
> char query_string[256]; /* holds constructed SQL query */
> PGconn *conn; /* holds database connection */
> PGresult *res; /* holds query result */
> int i;
>
> conn = PQconnectdb("dbname=wordpart"); /* connect to database */
>
> if (PQstatus(conn) == CONNECTION_BAD) /* did connection fail? */
> {
> fprintf(stderr, "Connection to database failed.\n");
> fprintf(stderr, "%s", PQerrorMessage(conn));
> exit(1);
> }
>
> printf("Type a word: "); /* prompt user for word to be analysed */
> scanf("%s", word);
>
> sprintf(query_string, "SELECT part FROM list WHERE word = '%s'",
> word); /* create SQL query string */
>
> res = PQexec(conn, query_string); /* sends the query */
>
> if (PQresultStatus(res) != PGRES_TUPLES_OK) /* did the query fail? */
> {
> fprintf(stderr, "SELECT query failed.\n");
> PQclear(res);
> PQfinish(conn);
> exit(1);
> }
>
> for (i = 0; i < PQntuples(res); i++) /* loop through all
> rows */
> printf("result = %s\n", PQgetvalue(res, i, 0)); /* display the
> value returned */


Delete these two lines. Instead of printf(), call PQgetvalue(res, i, 0) and
assign the result to a temporary pointer.

for (i = 0; i < PQntuples(res); i++)
{
const char *ptr;
ptr = PQgetvalues(res, i, 0);

/* now we can use ptr however we want */
e.g.
strcpy(part, ptr); /* copy the string to part */
printf("Query returned %s\n", ptr); /* print it out */
printf("Length %d\n", strlen(ptr)); /* get the length of the result */
if(!strcmp(ptr, "NOUN")) /* test it to use the value */
printf("It is a noun!");
}

On problem is that the query may return more than one value. It could be
that, because of the way your database is set up, you actually know that
PQNtuples() will always return 1. However words like "dream" could be either
nouns or verbs, so this might not be the case. As I have set things up,
"part" will be overwritten, and so set to the last value.
Unfortunately I cannot tell you how to handle this, since I don't know what
you wnat to achieve with the data.
>
>
> /* HERE IS WHERE I THINK THE ASSIGNMENT SHOULD BE; I want to 'paste'
> into 'part' */
>
>
> PQclear(res); /* free result */
>
> PQfinish(conn); /* disconnect from database */
>
> printf("%s = %s\n", word, part); /* a tester to check */
>
> return 0;
> }



 
Reply With Quote
 
Nicolao
Guest
Posts: n/a
 
      01-16-2005
In article <csd5m3$bg3$>, Malcolm
<> wrote:

> "Nicolao" <> wrote
> > I'm trying to write a program which retrieves data from a postgreSQL
> > database (e.g. I enter a word, and the program checks with the database
> > what part-of-speech it is). I've adapted a script I found and I can
> > retrieve the data fine, but I don't want to just display it, I want to
> > store it. >
> >
> > /* WORDFETCH.C, a program to consult database 'wordpart' */
> > /* compile like this: gcc -o wordfetch -I/usr/include/postgresql
> > wordfetch.c -lpq */
> >
> >
> > #include <stdio.h>
> > #include <stdlib.h>
> > #include "libpq-fe.h"
> >
> > int main()
> > {
> > char word[9]; /* holds word entered by user */
> > char part[12]; /* to hold copied part-of-speech from database */
> > char query_string[256]; /* holds constructed SQL query */
> > PGconn *conn; /* holds database connection */
> > PGresult *res; /* holds query result */
> > int i;
> >
> > conn = PQconnectdb("dbname=wordpart"); /* connect to database */
> >
> > if (PQstatus(conn) == CONNECTION_BAD) /* did connection fail? */
> > {
> > fprintf(stderr, "Connection to database failed.\n");
> > fprintf(stderr, "%s", PQerrorMessage(conn));
> > exit(1);
> > }
> >
> > printf("Type a word: "); /* prompt user for word to be analysed */
> > scanf("%s", word);
> >
> > sprintf(query_string, "SELECT part FROM list WHERE word = '%s'",
> > word); /* create SQL query string */
> >
> > res = PQexec(conn, query_string); /* sends the query */
> >
> > if (PQresultStatus(res) != PGRES_TUPLES_OK) /* did the query fail? */
> > {
> > fprintf(stderr, "SELECT query failed.\n");
> > PQclear(res);
> > PQfinish(conn);
> > exit(1);
> > }
> >
> > for (i = 0; i < PQntuples(res); i++) /* loop through all
> > rows */
> > printf("result = %s\n", PQgetvalue(res, i, 0)); /* display the
> > value returned */

>
> Delete these two lines. Instead of printf(), call PQgetvalue(res, i, 0) and
> assign the result to a temporary pointer.
>
> for (i = 0; i < PQntuples(res); i++)
> {
> const char *ptr;
> ptr = PQgetvalues(res, i, 0);
>
> /* now we can use ptr however we want */
> e.g.
> strcpy(part, ptr); /* copy the string to part */
> printf("Query returned %s\n", ptr); /* print it out */
> printf("Length %d\n", strlen(ptr)); /* get the length of the result */
> if(!strcmp(ptr, "NOUN")) /* test it to use the value */
> printf("It is a noun!");
> }
>
> On problem is that the query may return more than one value. It could be
> that, because of the way your database is set up, you actually know that
> PQNtuples() will always return 1. However words like "dream" could be either
> nouns or verbs, so this might not be the case. As I have set things up,
> "part" will be overwritten, and so set to the last value.
> Unfortunately I cannot tell you how to handle this, since I don't know what
> you wnat to achieve with the data.
> >
> >
> > /* HERE IS WHERE I THINK THE ASSIGNMENT SHOULD BE; I want to 'paste'
> > into 'part' */
> >
> >
> > PQclear(res); /* free result */
> >
> > PQfinish(conn); /* disconnect from database */
> >
> > printf("%s = %s\n", word, part); /* a tester to check */
> >
> > return 0;
> > }

>
>

Thanks a bunch, Malclom - and yes it's time I moved on to the chapter
about Arrays and Pointers!

As you can imagine, I have anticipated the problem of multiple uses of
the same character-strings (aka 'words'). At this point, my decision is
one-string-one-row, with special codes for multiples. Since I'm working
in English, and English is word-order dominated, my working hypothesis
is that I can make calculations on syntax through analysis of the
relative position in the sequence. And if that DOESN'T work out, then
the implications of that are interesting, from a viewpoint of Learning
Theory.

Yes, you guessed it - this is a Linguistics application, and at present
it's a) fun, and b) a real-world context for learning a programming
language. I like the idea of one sort of language assessing another.

So I shall implement your scheme, and doubtless leave the question of
PQNtuples() for somewhat later - if I can get a script that works! I'm
assuming I shall need to #include <string.h>.

My warmly genuine thanks to you for those pointers (sorry) and I'll
report back in case you're interested.

Nic
 
Reply With Quote
 
Nicolao
Guest
Posts: n/a
 
      01-17-2005
In article <160120052353250365%>, Nicolao <>
wrote:

> In article <csd5m3$bg3$>, Malcolm
> <> wrote:
>
> > "Nicolao" <> wrote
> > > I'm trying to write a program which retrieves data from a postgreSQL
> > > database (e.g. I enter a word, and the program checks with the database
> > > what part-of-speech it is). I've adapted a script I found and I can
> > > retrieve the data fine, but I don't want to just display it, I want to
> > > store it. >
> > >

snip
> >
> > Delete these two lines. Instead of printf(), call PQgetvalue(res, i, 0) and
> > assign the result to a temporary pointer.
> >
> > for (i = 0; i < PQntuples(res); i++)
> > {
> > const char *ptr;
> > ptr = PQgetvalues(res, i, 0);
> >
> > /* now we can use ptr however we want */



I tried that, but still have a problem. Here's how I modified the code,
and at the end why gcc refused it

#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"
#include <string.h>

int main()
{
char word[9]; /* holds word entered by user */
char part[12]; /* holds copied part-of-speech from
database */
char query_string[256]; /* holds constructed SQL query */
PGconn *conn; /* holds database connection */
PGresult *res; /* holds query result */
int i; /* for counting rows in database?? */

conn = PQconnectdb("dbname=wordpart"); /* connect to database */

if (PQstatus(conn) == CONNECTION_BAD) /* did connection fail? */
{
fprintf(stderr, "Connection to database failed.\n");
fprintf(stderr, "%s", PQerrorMessage(conn));
exit(1);
}

printf("Type a word: "); /* prompt user for word to be analysed */
scanf("%s", word);
sprintf(query_string, "SELECT part FROM list WHERE word = '%s'",
word); /* create SQL query string */
res = PQexec(conn, query_string); /* sends the query */

if (PQresultStatus(res) != PGRES_TUPLES_OK) /* did the query fail? */
{
fprintf(stderr, "SELECT query failed.\n");
PQclear(res);
PQfinish(conn);
exit(1);
}

for (i = 0; i < PQntuples(res); i++)
{
const char * ptr;
ptr = PQgetvalues(res, i, 0);
strcpy(part, ptr); /* copies the string to
part */

printf("Query returned %s\n", ptr); /* print it out */
printf("Length %d\n", strlen(ptr)); /* get the length of the
result */
printf("%s = %s\n\n", word, part);
}

PQclear(res); /* free result */
PQfinish(conn); /* disconnect from database */
return 0;
}

-------------

When I tried to compile this, gcc (Debian prerelease 2.95.4) refused:

wordgetfix.c In function 'main':
wordgetfix.c:45: warning: assignment makes pointer from integer without
a cast
/tmp/ccgSnYaQ.o: In function 'main':
/tmp/ccgSnYaQ.o(.text+0x160): undefined reference to 'PQgetvalues'
/tmp/ccgSnYaQ.o(.text+0x160): relocatipn truncated to fit: R_PPC_REL24
PQgetvalues


Any clues ??

Nic
 
Reply With Quote
 
Carlos
Guest
Posts: n/a
 
      01-17-2005
[Nicolao <>, Mon, 17 Jan 2005 11:44:48 GMT]
> I tried that, but still have a problem. Here's how I modified the code,
> and at the end why gcc refused it

[...]
> ptr = PQgetvalues(res, i, 0);


You wrote PQgetvalue in your first message.

[...]
> /tmp/ccgSnYaQ.o: In function 'main':
> /tmp/ccgSnYaQ.o(.text+0x160): undefined reference to 'PQgetvalues'
> /tmp/ccgSnYaQ.o(.text+0x160): relocatipn truncated to fit: R_PPC_REL24
> PQgetvalues

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      01-17-2005
Nicolao <> wrote:

> ptr = PQgetvalues(res, i, 0);


Let me guess. This is line 45, right?

> wordgetfix.c In function 'main':
> wordgetfix.c:45: warning: assignment makes pointer from integer without
> a cast
> /tmp/ccgSnYaQ.o: In function 'main':
> /tmp/ccgSnYaQ.o(.text+0x160): undefined reference to 'PQgetvalues'
> /tmp/ccgSnYaQ.o(.text+0x160): relocatipn truncated to fit: R_PPC_REL24
> PQgetvalues


And this is really just one error message, plus the result of it. The
real error is that you haven't defined PQgetvalues() - make sure that's
the correct spelling of the name. The result of this is that you don't
have a declaration for it, either, so the compiler is required to assume
that it returns an int - but then you assign it to a pointer, without a
cast. For the correct spelling of the name, with a correct declaration,
this is probably correct - but not for the automatic declaration of the
incorrect name.

Richard
 
Reply With Quote
 
Nicolao
Guest
Posts: n/a
 
      01-18-2005
In article <>, Richard Bos
<> wrote:

> Nicolao <> wrote:
>
> > ptr = PQgetvalues(res, i, 0);

>
> Let me guess. This is line 45, right?
>
> > wordgetfix.c In function 'main':
> > wordgetfix.c:45: warning: assignment makes pointer from integer without
> > a cast
> > /tmp/ccgSnYaQ.o: In function 'main':
> > /tmp/ccgSnYaQ.o(.text+0x160): undefined reference to 'PQgetvalues'
> > /tmp/ccgSnYaQ.o(.text+0x160): relocatipn truncated to fit: R_PPC_REL24
> > PQgetvalues

>
> And this is really just one error message, plus the result of it. The
> real error is that you haven't defined PQgetvalues() - make sure that's
> the correct spelling of the name. The result of this is that you don't
> have a declaration for it, either, so the compiler is required to assume
> that it returns an int - but then you assign it to a pointer, without a
> cast. For the correct spelling of the name, with a correct declaration,
> this is probably correct - but not for the automatic declaration of the
> incorrect name.
>
> Richard


Whoops, thrashing about here, am I not. And, however I correct what
I've done, I still get some kind of error. The problem - with this
being a rehash of someone else's program - is there's too much I don't
understand yet, like why none of those PQ* doodads is formally
declared, whereas the PG* ones are. Guess I should be patient and read
quite a bit more before biting off more than I can chew and then coming
to you guys for advice.

Thanks to all - for help and no less for the tactful indication of my
howlers!

Nic
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-18-2005
Nicolao wrote:
>

.... snip ...
>
> Thanks to all - for help and no less for the tactful indication of
> my howlers!


There's a first - calling us tactful. We are more used to being
called clumsy boorish inconsiderate tactless oafs when making
corrections and suggestions.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
Nicolao
Guest
Posts: n/a
 
      01-18-2005
In article <>, CBFalconer
<> wrote:

> Nicolao wrote:
> >

> ... snip ...
> >
> > Thanks to all - for help and no less for the tactful indication of
> > my howlers!

>
> There's a first - calling us tactful. We are more used to being
> called clumsy boorish inconsiderate tactless oafs when making
> corrections and suggestions.


Maybe another Newbie Error?

No way - I meant what I said.

Nic
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function decorator-like function vsoler Python 6 03-28-2010 04:26 PM
inline function vs function-like macro subramanian100in@yahoo.com, India C Programming 2 03-06-2007 04:43 AM
object-like macro used like function-like macro Patrick Kowalzick C++ 5 03-14-2006 03:30 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
What function in JScript like VBScript's Int() function W. Jack ASP General 5 05-13-2004 10:23 AM



Advertisments
 



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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57