fork download
  1. #include <fcntl.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #ifndef NULL
  7. #define NULL ((void *) 0)
  8. #endif
  9.  
  10. char* ext;
  11.  
  12. char *remove_ext (char* mystr) {
  13. char *retstr, *lastdot, *lastsep;
  14. char dot = '.';
  15. char slash = '/';
  16. char *temp;
  17. if (mystr == NULL)
  18. return NULL;
  19. if ((retstr = (char *)malloc (strlen (mystr) + 1)) == NULL)
  20. return NULL;
  21. strcpy (retstr, mystr);
  22. lastdot = strrchr (retstr, dot);
  23. lastsep = strrchr (retstr, slash);
  24. if (lastdot != NULL) {
  25. if (lastsep != NULL) {
  26. if (lastsep < lastdot) {
  27. *temp = *lastdot;
  28. ext = (char *)malloc (strlen(temp)+1);
  29. strcpy (ext, temp);
  30. *lastdot = '\0';
  31. }
  32. }
  33. else {
  34. // Has extension separator with no path separator.
  35. *lastdot = '\0';
  36. }
  37. }
  38. // Return the modified string.
  39. return retstr;
  40. }
  41.  
  42. int main(int argc, char* argv[]){
  43. char buf[128];
  44. int infile,fileread,outfile;
  45. infile = open(argv[1], O_RDONLY);
  46. char *filename,*newfile;
  47. filename = (char *)malloc(strlen(argv[1]+1));
  48. filename = remove_ext(argv[1]);
  49. newfile = (char *)malloc(strlen(argv[1]+3));
  50. int linecount=0;
  51. int filecounter = 0;
  52. while(fileread=read(infile, buf, sizeof(buf))>0)
  53. {
  54. sprintf(newfile,"%c%d%c",*filename,filecounter,*ext);
  55. outfile = open(newfile, O_CREAT | O_APPEND | O_RDWR);
  56. while(linecount<500)
  57. {
  58. if(fileread>0)
  59. {
  60. write(outfile, buf, sizeof(buf));
  61. linecount++;
  62. fileread=read(infile, buf, sizeof(buf));
  63. }
  64. }
  65. filecounter++;
  66. close(outfile);
  67. newfile = NULL;
  68. }
  69.  
  70. // lets open the input file
  71. close(infile);
  72. return 0;
  73. }
  74.  
Runtime error #stdin #stdout 0s 2156KB
stdin
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef NULL
#define NULL   ((void *) 0)
#endif

char* ext;

char *remove_ext (char* mystr) {
    char *retstr, *lastdot, *lastsep;
    char dot = '.';
    char slash = '/';
	char *temp;
    if (mystr == NULL)
    return NULL;
    if ((retstr = (char *)malloc (strlen (mystr) + 1)) == NULL)
    return NULL;
    strcpy (retstr, mystr);
    lastdot = strrchr (retstr, dot);
    lastsep = strrchr (retstr, slash);
    if (lastdot != NULL) {
        if (lastsep != NULL) {
            if (lastsep < lastdot) {
				*temp = *lastdot;
                ext = (char *)malloc (strlen(temp)+1);
                strcpy (ext, temp);
                *lastdot = '\0';
            }
        }
        else {
            // Has extension separator with no path separator.
            *lastdot = '\0';
        }
    }
    // Return the modified string.
    return retstr;
}

int main(int argc, char* argv[]){
    char buf[128];
    int infile,fileread,outfile;
	infile = open(argv[1], O_RDONLY);
    char *filename,*newfile;
    filename = (char *)malloc(strlen(argv[1]+1));
    filename = remove_ext(argv[1]);
    newfile = (char *)malloc(strlen(argv[1]+3));
	int linecount=0;
    int filecounter = 0;
    while(fileread=read(infile, buf, sizeof(buf))>0)
    {
        sprintf(newfile,"%c%d%c",*filename,filecounter,*ext);
		outfile = open(newfile, O_CREAT | O_APPEND | O_RDWR);
        while(linecount<500)
        {
            if(fileread>0)
            {
                write(outfile, buf, sizeof(buf));
                linecount++;
                fileread=read(infile, buf, sizeof(buf));
            }
        }
        filecounter++;
		close(outfile);
		newfile = NULL;
    }
    
    // lets open the input file
	close(infile);
	return 0;
}
stdout
Standard output is empty