473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Help with strings

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

Nov 14 '05 #1
1 1432

<jo******@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jack Smith | last post by:
Can some one help me with this proof? Let L be the language of strings over {a,b} recursively defined by the following set of rules (i) the string ^ is in L (^ is the null string). (ii) for...
5
by: ArShAm | last post by:
Hi there Please help me to optimize this code for speed I added /O2 to compiler settings I added /Oe to compiler settings for accepting register type request , but it seems that is not allowed...
3
by: Mahmood Ahmad | last post by:
Hello, I have written a program that reads three types of records, validates them acording to certain requirements and writes the valid records into a binary file. The invalid records are...
0
by: Jon | last post by:
We have a asp.net app that runs like the following in a single-site scenario. Directory structure: c:\inetpub\wwwroot\myapp - aspx files, global.asax and web.config c:\inetpub\wwwroot\myapp\bin...
9
by: Diane | last post by:
Could you please explain me how can I output nested strings? Here is an example: "adsd{rfkm}xcv" The output should start from the inner parentheses, such as: dfF rfkm
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
4
by: Robin Haswell | last post by:
Okay I'm getting really frustrated with Python's Unicode handling, I'm trying everything I can think of an I can't escape Unicode(En|De)codeError no matter what I try. Could someone explain to...
2
by: almurph | last post by:
Hi, Hope you can help me. This is a trick one - at least I think so. I have 2 strings. I have to calculate the "score" of the strings. Score is determined by matching patterns of letters within...
4
by: Debbiedo | last post by:
My software program outputs an XML Driving Directions file that I need to input into an Access table (although if need be I can import a dbf or xls) so that I can relate one of the fields...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.