Hello Every one,
I need to write a program to copy a part of a String from a specified
place.
Eg: if the first String looks like "This is a C program"
I need to extract the part of "C program"
Can I modify the below program

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

int main()
{
FILE *f;
char i=0;
char *c;
char d[256];
if((f=fopen("D:\\data\\def.txt","r"))==NULL){
printf("File can't open.\n");
}
else{
while((c=fgetc(f))!=EOF){

printf("%c",c);
}

}
}
4
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by yellowSnow

Use strtok().

Hello Every one,
I need to write a program to copy a part of a String from a specified
place.
Eg: if the first String looks like "This is a C program"
I need to extract the part of "C program"
Can I modify the below program

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

int main()
{
FILE *f;
char i=0;
char *c;
char d[256];
if((f=fopen("D:\\data\\def.txt","r"))==NULL){
printf("File can't open.\n");
}
else{
while((c=fgetc(f))!=EOF){

printf("%c",c);
}

}
}

I don't mind helping you out, but you really need to start learning how to do your own research. With this question you're dealing with strings so this would suggest that you need to look at the library documentation for string handling functions. This is an essential and valuable skill to learn if you wish to pursue a career in the software development field.

For this particular example, in the first instance, you should look at the strstr() function. I'm not sure what modifications you're looking for in your code snippet. Like your previous post, you have forgotten to return an integer from your main() function (i.e. return 0).

Use strtok().

The strtok() function is a poorly implemented function and except for the absolute simplest cases (or for demonstration purposes) should be avoided. There are plenty of online references as to the pitfalls of this function.

Unless I misinterpreted the OP's question, it didn't sound like he/she was looking to tokenise the string in any case.

just use strstr and use its return value to copy it