Solved

String cutting ?

Posted on 1997-12-05
3
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 84

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 70 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

626 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