Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

String cutting ?

Posted on 1997-12-05
3
Medium Priority
?
279 Views
Last Modified: 2010-04-02
Im trying to get a sample source code, that will tell me how to read a files 3rd line, save it to a string, and then cut off the last 4 characters of that string. Thanks for any help!
0
Comment
Question by:lint17
3 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 1256524
#include <stdio.h>
main(){
       char string[1000];
        int i;
        fgets (string, 999, stdin);
        fgets (string, 999, stdin);
        fgets (string, 999, stdin);
      if( (i = strlen(string)) >= 4 ){ string[i-4] = '\0'; };
      printf("'%s'\n",string);
}

0
 
LVL 10

Accepted Solution

by:
rbr earned 210 total points
ID: 1256525
main ()
{
   char buffer[1000],string[5];
   FILE *pfile;

   pfile=fopen ("name","r");

   fgets (string,999,pfile);
   fgets (string,999,pfile);
   fgets (string,999,pfile);
   if (strlen (buffer) > 4) {
       strcpy (string,&(buffer[strlen(buffer)-4]));
   } else {
       strcpy (string,buffer);
   }
}
       

0
 

Author Comment

by:lint17
ID: 1256526
Thanks, but your program didnt quite work totaly.
I just changed some of the stuff and it worked FINE!
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question