473,781 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving string out of token and put in array

1 New Member
Hi all,

I am currently trying to retrieve a string from a textfile, separate that and put it in an array.

I stumbled on the problem that *token is a pointer, and not the string. However when i printf("%s", token) it prints out the word that it points to. Could anyone explain how to fix this problem?

Words.txt:

dog|cat|hamster

The code:
char c[50000]; /* declare a char array */
int n;
FILE *file; /* declare a FILE pointer */
file = fopen("words.tx t", "r");
n = fread(c, 1, 50000, file);
c[n] = '\0';

/* Separating the contents, divided by | */

char *token = strtok(c, "|");
char words[50000];
int f = 0;
while(token != NULL)
{
printf("[%s]\n", token);
/* Here the error is caused: invalid conversion from char* to char */
words[f] = token;
token = strtok(NULL, "|");
f++;
}
fclose(file);
Dec 21 '09 #1
1 3796
weaknessforcats
9,208 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1. char *token = strtok(c, "|");
*token is not a pointer. The code above says that *token is a char.

It is token that is a pointer of char.

When C functions see a char*, they usually assume a null-termninated string. So printf displays the string atthe address in token.

This code:

Expand|Select|Wrap|Line Numbers
  1. char words[50000];
  2. int f = 0;
  3. while(token != NULL)
  4. {
  5. printf("[%s]\n", token);
  6. /* Here the error is caused: invalid conversion from char* to char */
  7. words[f] = token;

attempts to assign a pointer to char (token) to a char. Hence, the invalid conversion from char* to char.

I expect you want to copy the string pointed at by token to the words array.

If so, you need a) an array of char* rather than char, b) you need to allocate memory for words[f] and finally strcpy from token to words[f] to place the string in the array.
Dec 21 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
18432
by: Lans | last post by:
I have a string that I need to tokenize but I need to use a string token see example i am trying the following but strtok only uses characters as delimiters and I need to seperate bu a certain word char *mystring "Jane and Peter and Tom and Cindy" char *delim = " and "; char *token; token = strtok(mystring, delim);
1
3330
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value for my first element but my subsequent printf's will return garbage. Can someone let me know what I am doing wrong. Thanks in advance. -jlewis
9
2048
by: robbie.carlton | last post by:
Hello! I've programmed in c a bit, but nothing very complicated. I've just come back to it after a long sojourn in the lands of functional programming and am completely stumped on a very simple function I'm trying to write. I'm writing a function that takes a string, and returns an array of strings which are the result of splitting the input on whitespace and parentheses (but the parentheses should also be included in the array as...
4
3387
by: dchan1 | last post by:
String line = " I am new to c# "; How to use Regex to split the string so that It would return a String array with: token = "I" token = "am" token = "new" token = "to" token = "c#"
2
8712
by: bluebeta | last post by:
Hi, I am using embedded visual C++ 4.0 and I want to use function strtok to split string into array. my sample code is as below: ---------------------------------------------------------------- void CNEWSERIALNO::OnBInsert() { // TODO: Add your control notification handler code here
16
2043
by: Amit Gupta | last post by:
Hi - I get a seg-fault when I compile and run this simple program. (seg-fault in first call to strtok). Any clues? My gcc is "gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)" #include <string.h> int main()
2
12069
by: Kelly B | last post by:
I tried to write a code which would reverse the order of words in a given string. I.e if Input String=The C Programming Language Output String=Language Programming C The Here is the code ..but it seems a bit "bloated" as i need an extra array of same size of the original string
1
3273
by: nganglove | last post by:
C++ string search -------------------------------------------------------------------------------- Hello, please can any one help me? I am given an assigment in C++ to read a text file and search for certain words.If those stated words are found, then I should print the enire them including the entire line. Below is an example of such a text file Example text file: Possible failures: -> Member=1, FSce=1, MP=13.16%, atching...
6
1866
by: giffy | last post by:
HOW to get the spaces out of a delimited string ?? here is a usage scenario where split() seems not to work String origAVNListStr = "~`L~`L~`L~`~`~`L~`"; String strArr = origAVNListStr.split("~`"); for(int i=0;i<strArr.length;i++) { System.out.println(i+1 +"the next token - " + strArr); } and the output is
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8964
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.