How to get first and last word from lines in C? - c

I have a problem with code. I want to take first and last words from each lines from txt file. I wrote this code for now:
void StartEnd(char * word)
{
FILE* fp;
fp = fopen("linie.txt", "r");
if (fp == NULL) {
printf("! ");
return;
}
char store[MAX_LINE];
while (fgets(store, MAX_LINE - 1, fp)){
char * FirstWord = strtok(store," ");
char * LastWord;
char * token = strtok (store," ");
while (token != NULL){
LastWord = token;
token = strtok (NULL," ");
}
printf("%s\n",LastWord);
}
fclose(fp);
}
It is working for last word but only if i dont use FirstWord with strtok and i dont know why :(.
I would be grateful for any answer. Thanks!

On a first call, the function expects a C string as argument for str,
whose first character is used as the starting location to scan for
tokens. In subsequent calls, the function expects a null pointer and
uses the position right after the end of the last token as the new
starting location for scanning.
On subsequent calls to strtok, you are supposed to pass a NULL pointer. Doing that makes the function work properly:
void StartEnd(char * word)
{
FILE* fp;
fp = fopen("linie.txt", "r");
if (fp == NULL) {
printf("! ");
return;
}
char store[MAX_LINE];
while (fgets(store, MAX_LINE - 1, fp)){
char * FirstWord = strtok(store," ");
char * LastWord;
char * token = strtok (NULL, " ");
while (token != NULL){
LastWord = token;
token = strtok (NULL," ");
}
printf("%s\n",LastWord);
printf("%s\n",FirstWord);
}
fclose(fp);
}
Keep in mind that fgets reads n-1 characters or until it encounters a newline or EOF character. however a newline character is also considered a valid character and stored in the string. That means LastWord may end with a newline character. To fix that, pass " \n" instead of " " to strtok. Then both a newline and a blankspace are considered delimiters.

Related

Parsing a text file with different types in C

I'm having some trouble when parsing a text file. Each line of the text has a name followed after three float values. All of them are separated by a blankspace. What I want is to store the name in a string and the numbers in an array. I know I have to read each line using fgets and then strtok but the thing is I don't understand how strtok works. Do I have to call strtok four times? How do I assign each "piece" to my variables ?
Thank you for your time!
The strtok will search for the given tokens in a string. You must call it until the it returns NULL.
char *strtok(char *str, const char *delim)
The first call is done passing the string (char*) as the argument str and the remaining times are done passing NULL, as this will define that it should keep looking for the next token from that point onwards.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char line[] = "name 1.45 2.55 3.65";
char* name;
double values[3] = { 0 };
char* ptr = NULL;
int i = 0;
ptr = strtok(line, " "); // Search for the first whitespace
if (ptr != NULL) // Whitespace found
{
/* 'line' is now a string with all the text until the whitespace,
with terminating null character */
name = calloc(1, strlen(line));
strcpy(name, line);
while ((i < 3) && (ptr != NULL))
{
ptr = strtok(NULL, " "); // Call 'strtok' with NULL to continue until the next whitespace
if (ptr != NULL) // Whitespace found
{
/* 'ptr' has now the text from the previous token to the found whitespace,
with terminating null character */
values[i] = atof(ptr); // Convert to double
}
i++;
}
}
printf("%s %lf %lf %lf\n", name, values[0], values[1], values[2]);
}

Using fgets in combination with strtok to transform input to tokens

I am reading input from different text files. These text files are integers that are either separated by a space, a new line, or a combination of spaces and new lines. I want to convert these strings to integers, and use these integers for a sorting algorithm.
char *line = malloc(BUF_SIZE);
char *token;
struct list* l = list_init();
while (fgets(buf, BUF_SIZE, stdin)) {
token = strtok(buf," \n");
printf("%s", token);
}
list_cleanup(l);
return 0;
What is wrong with this, it that it just prints the first element of each line. It doesn't handle multiple elements per line.
Thanks in advance.
You need to have loop to process all the tokens. strtok will return NULL once all the tokens are over.
Example:
while (fgets(buf, BUF_SIZE, stdin)) {
token = strtok(buf," \n");
while (token != NULL) {
printf("%s", token);
token = strtok(NULL," \n");
}
}

strtok changing value of pointer

i have the following code:
char* pathTokens;
char* paths;
paths = getFilePaths();
//printf("%s", paths);
pathTokens = strtok(paths, "\n");
updateFile(pathTokens, argv[1]);
and these variables in the same file as updateFile():
static FILE* file;
static char content[1024];
static char* token;
static int numChanges = 0;
static char newContent[1024];
Here is updateFile():
void updateFile(char pathTokens[], char searchWord[]) {
while(pathTokens != NULL) {
printf("Token: %s\n", pathTokens);
updateNewContent(pathTokens, searchWord);
pathTokens = strtok(NULL, "\n");
}
}
and updateNewContent():
static void updateNewContent(char fileName[], char searchWord[]) {
if(searchWord == NULL) {
printf("Please enter a word\n");
return;
}
numChanges = 0;
file = fopen(fileName, "r");
if(file == NULL) {
printf("Error opening file\n");
return;
}
while(fgets(content, 1024, file) != NULL) {
token = strtok(content, " ");
}
fclose(file);
}
whenever token = strtok(content, " "); is called, the value of pathTokens changes. if i comment it out, pathTokens maintains its original values. i don't want pathTokens to change, so why is strtok modifying it?
You are nesting strtok calls and strtok doesn't work like that. For nesting
calls you have to use strtok_r.
Also, when calling strtok, only the first time the source argument must be
used, for all subsequent calls, NULL has to be used. When you call strtok
again with an non-NULL argument, strtok "forgets" about the last state and
"restarts" parsing new content.
When you do in updateNewContent you are doing:
while(fgets(content, 1024, file) != NULL) {
token = strtok(content, " ");
}
strtok will forget about paths (the very first call). Also this loop is
pointless, you read a line, you split it for the first time, and then read the
next line, split it again, etc. You are doing nothing with token. When the
loop ends token will store the first word of the last line.
And then the function returns and you do
pathTokens = strtok(NULL, "\n");
Because you call it with NULL, it will look continue parsing the contents
pointed to by content, which seems to be a global variable.
whenever token = strtok(content, " "); is called, the value of pathTokens changes
Of course it does, after updateNewContent returns, you assign a new value to
it. What else did you expect?
I really don't know what you are trying to do here, to me that makes no sense.
If you need to do a strtok with a token that previously returned by another
strtok, then you have to use strtok_r.
Here is an example of how to nest strtok:
char line[] = "a:b:c,d:e:f,x:y:z";
char *s1, *s2, *token1, *token2, *in1, *in2;
in1 = line;
while(token1 = strtok_r(in1, ",", &s1))
{
in1 = NULL; // for subsequent calls
in2 = token1;
printf("First block: %s\n", token1);
while(token2 = strtok_r(in2, ":", &s2))
{
in2 = NULL; // for subsequent calls
printf(" val: %s\n", token2);
}
}
Output:
First block: a:b:c
val: a
val: b
val: c
First block: d:e:f
val: d
val: e
val: f
First block: x:y:z
val: x
val: y
val: z
If you use strtok() function it means that you want to divide your input into tokens. Like that when you given input strtok(pathtokens,"") ,divides into tokens and prints even though there is pointer variable

Get each word from a line in a text file

I am trying to read a txt file, and I can get the line which I want, but I can not print every words in this line one by one;
for example: the line looks like:
hello world 1 2 3
and I need print them one by one which looks like:
hello
world
1
2
3
I got the segmentation fault core dumped error
char temp[256];
while(fgets(temp, 256, fp) != NULL) {
...
int tempLength = strlen(temp);
char *tempCopy = (char*) calloc(tempLength + 1, sizeof(char));
strncpy(temCopy, temp, tempLength); // segmentation fault core dumped here;
// works fine with temp as "name country"
name = strtok_r(tempCopy, delimiter, &context);
country = strtok_r(Null, delimiter, &context);
printf("%s\n", name);
printf("%s\n", country);
}
Can anyone help me fix the code?
Thanks!
Impleted with strtok()
char *p;
char temp[256];
while(fgets(temp,256,fp) != NULL){
p = strtok (temp," ");
while (p != NULL)
{
printf ("%s\n",p);
p = strtok (NULL, " ");
}
}
If you see man strtok You will found
BUGS
Be cautious when using these functions. If you do use them, note that:
* These functions modify their first argument.
* These functions cannot be used on constant strings.
* The identity of the delimiting character is lost.
* The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.
Try to make changes with strtok_r()
While read a line from a file you can invoke the following function:
if( fgets (str, 60, fp)!=NULL ) {
puts(str);
token = strtok(str," ");
while(token != NULL)
{
printf("%s\n",token);
token = strtok(NULL," ");
}
}

How would i Use strtok to compare word by word

I've been reading up on strtok and thought it would be the best way for me to compare two files word by word. So far i can't really figure out how i would do it though
Here is my function that perfoms it:
int wordcmp(FILE *fp1, FILE *fp2)
{
char *s1;
char *s2;
char *tok;
char *tok2;
char line[BUFSIZE];
char line2[BUFSIZE];
char comp1[BUFSIZE];
char comp2[BUFSIZE];
char temp[BUFSIZE];
int word = 1;
size_t i = 0;
while((s1 = fgets(line,BUFSIZE, fp1)) && (s2 = fgets(line2,BUFSIZE, fp2)))
{
;
}
tok = strtok(line, " ");
tok2 = strtok(line, " ");
while(tok != NULL)
{
tok = strtok (NULL, " ");
}
return 0;
}
Don't mind the unused variables, I've been at this for 3 hours and have tried all possible ways I can think of to compare the values of the first and second strtok. Also I would to know how i would check which file reaches EOF first.
when i tried
if(s1 == EOF && s2 != EOF)
{
return -1;
}
It returns -1 even when the files are the same! Is it because in order for it to reach the if statement outside of the loop both files have reached EOF which makes the program always go to this if statement?
Thanks in advance!
If you want to check if files are same try doing,
do {
s1 = fgetc(fp1);
s2 = fgetc(fp2);
if (s1 == s2) {
if (s1 == EOF) {
return 1; // RETURN TRUE
}
continue;
}
else {
return -1; // RETURN FALSE
}
} while (1);
Good Luck :)
When you use strtok() you typically use code like this:
tok = strtok(line, " ");
while (NULL != tok)
{
tok = strtok(NULL, " ");
}
The NULL in the call in the loop tells strtok to continue from after the previously found token until it finds the null terminating character in the value you originally passed (line) or until there are no more tokens. The current pointer is stored in the run time library, and once strtok() returns NULL to indicate no more tokens any more calls to strtok() using NULL as the first parameter (to continue) will result in NULL. You need to call it with another value (e.g. another call to strtok(line, " ")) to get it to start again.
What this means is that to use strtok on two different strings at the same time you need to manually update the string position and pass in a modified value on each call.
tok = strtok(line, " ");
tok2 = strtok(line2, " ");
while (NULL != tok && NULL != tok2)
{
/* Do stuff with tok and tok2 here */
if (strcmp(tok, tok2)... {}
/* Update strtok pointers */
tok += strlen(tok) + 1;
tok2 += strlen(tok2) + 1;
/* Get next token */
tok = strtok(tok, " ");
tok2 = strtok(tok2, " ");
}
You'll still need to add logic for determining whether lines are different - you've not said whether the files are equivalent if a line break occurs at different position but the words surrounding it are the same. I assume it should be, given your description, but it makes the logic more awkward as you only need to perform the initial fgets() and strtok() for a file if you don't already have a token. You also need to look at how files are read in. Currently your first while loop just reads lines until the end of the file without processing them.

Resources