Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char *readText()
- {
- char *data = malloc(sizeof(char));
- char c;
- int i = 0;
- fread(&c, 1, 1, stdin);
- while(c != '\n'){
- data = realloc(data, i+2);
- data[i] = c;
- i++;
- fread(&c, 1, 1, stdin);
- }
- data[i] = '\0';
- return data;
- }
- int main()
- {
- do{
- printf("> Type 'quit' to end testing.\n");
- printf("> Enter the name of file: ");
- char *words = readText();
- if(!strcmp(words, "quit")){
- free(words);
- return 0;
- }
- int extLen = 0;
- int nameLen;
- int total;
- if(words[0] == '.'){
- nameLen = strlen(words);
- char orig[nameLen];
- strcpy(orig, words);
- words++;
- char *ext = strrchr(words, '.');
- if(ext){
- extLen = strlen(ext);
- if(extLen == 1){
- printf("Extension: '(none)'\n");
- }else{
- printf("Extension: '%s'\n", ext+1);
- }
- }else{
- printf("Extension: '(none)'\n");
- }
- total = nameLen-extLen;
- char name[total];
- strncpy(name, orig, total);
- name[total] = '\0';
- printf("name: '%s'\n", name);
- }else{
- nameLen = strlen(words);
- char *ext = strrchr(words, '.');
- if(ext){
- extLen = strlen(ext);
- if(extLen == 1){
- printf("Extension: '(none)'\n");
- }else{
- printf("Extension: '%s'\n", ext+1);
- }
- }else{
- printf("Extension: '(none)'\n");
- }
- total = nameLen-extLen;
- char name[total];
- strncpy(name, words, total);
- name[total] = '\0';
- if(name[0] == '\0'){
- printf("name: '(none)'\n");
- }else{
- printf("name: '%s'\n", name);
- }
- }
- free(words);
- puts("------------------------");
- puts("------------------------");
- }while(1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement