Help with strings

J

john_g83

Hello all, new user to c programming and am having difficulty with a
little program I am trying to write. Basically what I have is a file
which contains a list of words i.e:

tst.txt:

customer
customers
data
deactivate
deactivated
department
department
dept.

etc....

I want to read in each word from the file Individually, and assign each
individual word to an individual element in an array.
the code I have so far is:

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

main()
{
int c;
FILE *in_file;
FILE *out_file;
FILE *out;

in_file = fopen ("thes1.txt", "r");

if( in_file == NULL )
printf("Cannot open %s for reading.\n");
else
out_file = fopen ("tst.txt", "w");
if( out_file == NULL )
printf("Can't open %s for writing.\n");
else {
while( (c = getc( in_file)) != EOF )
{
putc (c, out_file);
}

putc (c, out_file); /* copy EOF */
printf("File has been copied.\n");

}
fclose (in_file);
fclose (out_file);

out = fopen("tst.txt","r");

int zz=0;
char key2[20];

while(!feof(out))
{
fgets(key2,20,out);
printf("%s\n",key2);

}

}

this code will get each word Individually, but so far I have not been
able to assign words to elements in an array, continiously getting
errors about invalid conversions from char*. Any help would be greatly
appreciated.
Kind Regards
 
P

Paulie

Hello all, new user to c programming and am having difficulty with a
little program I am trying to write. Basically what I have is a file
which contains a list of words i.e:

tst.txt:

customer
customers
data
deactivate
deactivated
department
department
dept.

etc....

I want to read in each word from the file Individually, and assign each
individual word to an individual element in an array.
the code I have so far is:

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

main()
{
int c;
FILE *in_file;
FILE *out_file;
FILE *out;

in_file = fopen ("thes1.txt", "r");

if( in_file == NULL )
printf("Cannot open %s for reading.\n");
else
out_file = fopen ("tst.txt", "w");
if( out_file == NULL )
printf("Can't open %s for writing.\n");
else {
while( (c = getc( in_file)) != EOF )
{
putc (c, out_file);
}

putc (c, out_file); /* copy EOF */
printf("File has been copied.\n");

}
fclose (in_file);
fclose (out_file);

out = fopen("tst.txt","r");

int zz=0;
char key2[20];

while(!feof(out))
{
fgets(key2,20,out);
printf("%s\n",key2);

}

}

this code will get each word Individually, but so far I have not been
able to assign words to elements in an array, continiously getting
errors about invalid conversions from char*. Any help would be greatly
appreciated.
Kind Regards

I'm not exactly sure what your trying to do but if you are attempting to
assign to an array directly you cannot do it in C.

C does not have strings, it has arrays of characters terminated with a null
character which is actually binary 0 represented as '\0'. However most
people call them strings. There are many string functions to manipulate
character arrays, they all work on the idea of

1. The name of the array is the address of the array.
2. The array will be terminated with a binary 0 to indicate the end of the
text.

Or there are a group of 'mem' functions to work with data. Take a look at
the strcpy/strncpy and memcpy functions. Make sure you understand how they
work and differences between them.

char xxx[10] = "DAVE"; OK at initialisation
xxx = "PAUL" is invalid - xxx is address of array,
"PAUL" is constant string.
xxx[0] = 'P'; OK;
xxx[1] = "AUL"; Invalid, xxx[1] is a character, "AUL" is a
string

On a separate note, you need to take a look at your error checking when
opening the two files at the top.

in_file = fopen ("thes1.txt", "r");
if( in_file == NULL )
printf("Cannot open %s for reading.\n");
else
out_file = fopen ("tst.txt", "w");
if( out_file == NULL )
printf("Can't open %s for writing.\n");

Ask yourself - what is going to happen if the fopen of "thes1.txt" fails?
Is out_file going to be NULL? It probably works at at the moment because
"thes1.txt" exists - try renaming or deleting it and run the program again.

HTH, Paul
 
P

Peter Shaggy Haywood

Groovy hepcat (e-mail address removed) was jivin' on 19 May 2005 11:34:45
-0700 in comp.lang.c.
Help with strings's a cool scene! Dig it!
Hello all, new user to c programming and am having difficulty with a
little program I am trying to write. Basically what I have is a file
which contains a list of words i.e:

tst.txt:

customer
customers
data
deactivate
deactivated
department
department
dept.

etc....

I want to read in each word from the file Individually, and assign each
individual word to an individual element in an array.
the code I have so far is:

[Snip.]

Ick! What ugly code! Code that is badly formatted is dificult to
read and, therefore, hard to understand. Many people on Usenet will
simply ignore code that looks a mess, as yours does. In future please
try to write neat, well formatted code. Here I have cleaned up your
code, and will point out some problems and errors in it.

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

main()
{
int c;
FILE *in_file;
FILE *out_file;
FILE *out;

in_file = fopen ("thes1.txt", "r");

if( in_file == NULL )
printf("Cannot open %s for reading.\n");
else
out_file = fopen ("tst.txt", "w");

if( out_file == NULL )
printf("Can't open %s for writing.\n");
else
{
while( (c = getc( in_file)) != EOF )
{
putc (c, out_file);
}

putc (c, out_file); /* copy EOF */
printf("File has been copied.\n");
}

fclose (in_file);
fclose (out_file);

out = fopen("tst.txt","r");

int zz=0;
char key2[20];

while(!feof(out))
{
fgets(key2,20,out);
printf("%s\n",key2);
}
}

First of all, you can see how much clearer this code is now that
I've cleaned it up. There are some problems immediately obvious. Let's
look at the code again, piece by piece.
#include <stdio.h>
#include <string.h>

main()

There is something wrong with this line. You have failed to specify
the return type. This defaults to int in C90. This is often refered to
as "implicit int". However, C99 forbids this. You must specify the
return type to satisfy all C99 implementations. It was always a good
idea in C90 anyhow, even if only for clarity. It's always a good idea
to specify the parameter list too, even if it's only void. So, the
above line of code should be:

int main(void)

Now, moving right along...
{
int c;
FILE *in_file;
FILE *out_file;
FILE *out;

in_file = fopen ("thes1.txt", "r");

if( in_file == NULL )
printf("Cannot open %s for reading.\n");
else
out_file = fopen ("tst.txt", "w");

First of all, diagnostic output usually goes to stderr, not stdout.
But that's the least of your worries here.
If the first fopen() call fails all you're doing is printing a
diagnostic message. You're not doing anything else to avoid trying to
access the file via a null pointer. In the subsequent lines of code
you dereference this null pointer, causing undefined behaviour. It
looks like your intention was to put the rest of the code in the
"else" clause. You have failed to do so, however.
if( out_file == NULL )

Here out_file may or may not have been assigned a value, depending
on whether the first fopen() call succeeded.
printf("Can't open %s for writing.\n");
else
{
while( (c = getc( in_file)) != EOF )

Again, because this code is executed regardless of the value of
in_file, you may be dereferencing a null pointer here.
{
putc (c, out_file);

And here the value of out_file is unknown. It could be the address
of a valid FILE, or it could be uninitialised.
}

putc (c, out_file); /* copy EOF */

Huh? What on Earth? Why are you attempting to write EOF to the file?
This doen't make any sense. You may find some strange character in
your file. But that's just one possibility. EOF is a negative integer
that does not compare equal to any member of the execution character
set. What this means is that you're trying to output something that is
not a valid character. What happens within putc() is that this value
is converted to unsigned char, and the resulting value is output. This
could be a valid printable character, in which case you see an
unexpected character in your file. It could be a control character, in
which case the system may interpret it somehow and do something you
don't expect. Or it might not be a valid character, in which case...
who knows?
So, anyhow, don't do that.
printf("File has been copied.\n");
}

fclose (in_file);
fclose (out_file);

This is bad. You can't be sure, at this point in the code, that
either in_file or out_file is valid (let alone both of them). in_file
is either valid or null; and out_file is either valid, null or
uninitialised. Passing them to fclose() could be disastrous. Undefined
behaviour ensues if eitehr of these is not valid.
out = fopen("tst.txt","r");

Why use an extra variable? You already have two FILE * variables
you've already finished with. You could use one of them. Just a
suggestion.
int zz=0;
char key2[20];

In C90 you cannot put variable declarations after statements. This
is allowed in C99, but your code is already broken as far as C99 is
concerned. (Remember, C99 forbids implicit int.) And now it is broken
as far as C90 is concerned too. This program will not compile without
errors on any standard (C90 or C99) conforming C compiler.
while(!feof(out))

Having read the FAQ you know why this is no way to control a file
reading loop, of course. You *have* read the FAQ, haven't you? (That's
a rhetorical question. I know you haven't. But I hope you remedy that
situation post haste.)
{
fgets(key2,20,out);

It's best to avoid what we call "magic numbers" in code. These are
literal numbers, such as the 20 in the above line of code. It is
better to use something meaningful. Since key2 is an array, you can
use sizeof key2 instead of 20.
printf("%s\n",key2);
}

Since this function has a return type of int, you should return an
int. Portable return values of main() are 0, EXIT_SUCCESS and
EXIT_FAILURE (the latter two being macros defined in stdlib.h). The
following will suffice:

return 0;
this code will get each word Individually, but so far I have not been
able to assign words to elements in an array, continiously getting
errors about invalid conversions from char*. Any help would be greatly
appreciated.

You haven't shown how you are trying to "assign words to elements in
an array", nor what you mean by this exactly. There are several ways
to add strings to an array.
One thing I notice is that you are making a copy of the file, one
character at a time, and then reading the copy, one line at a time.
Why? If all you want to do is read in the contents of the file, why
copy it?

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Working with files 1
Replace word in text file 18
Need help with this script 4
URGENT 1
Help with code 0
C language. work with text 3
Why can't use fgetc() in SWITCH with CASE? 15
please help !! 8

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top