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"
How can I do this.Please help me.
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);
		}	

	}
}

Recommended Answers

All 6 Replies

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"
How can I do this.Please help me.
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

commented: Stop posting for the sake of posting .. it's annoying! -1

or maybe read hole the txt into a string and then use strstr something like this

just use strstr and use its return value to copy it

You really like posting answers that have already been suggested. Do you actually read all the posts in a thread? I don't think so!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.