1

I am trying to create a C program which has a text.txt file with 50 lines. This text.txt file should be split into 5 files such as text_part1.txt, text_part2.txt and so on. The 50 lines in the text.txt file should be copied equally to 10 lines each in 5 files.

All these has to be done by using command line arguments. I am a beginner in C and have just started to code. I don't know how to use command line arguments.

#include <stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
 FILE *ptr_readfile;
 FILE *ptr_writefile;
 char line [100]; 
 char fileoutputname[10];
 int filecounter=1, linecounter=1;

 ptr_readfile = fopen("C:/home/dir/sample_pg/data/text.txt","r");
 if (!ptr_readfile)
 return 1;

 sprintf(fileoutputname, "file_part%d", filecounter);
 ptr_writefile = fopen(fileoutputname, "w");

 while (fgets(line, sizeof line, ptr_readfile)!=NULL) 
 {
    if (linecounter == 5)
        {
        fclose(ptr_writefile);
        linecounter = 1;
        filecounter++;
        sprintf(fileoutputname, "file_part%d", filecounter);
        ptr_writefile = fopen(fileoutputname, "w");
        if (!ptr_writefile)
            return 1;
        }
    fprintf(ptr_writefile,"%s\n", line);
    linecounter++;
 }
 fclose(ptr_readfile);
 return 0;
 }
5
  • This link is the first one to come up in a google search. Commented Jun 25, 2015 at 4:26
  • okay...Thnaks for your time, in replying immediately. Please tell me whether the code is correct to create multiple files because i dont find any text file in the specified path Commented Jun 25, 2015 at 4:27
  • You need to compile with all warnings & debug info (e.g. gcc -Wall -Wextra -g if using GCC....) then learn how to use the debugger Commented Jun 25, 2015 at 4:28
  • Your program has bugs. It is your duty to find them. Commented Jun 25, 2015 at 4:36
  • If my answer fits, please accept it. Commented Jun 25, 2015 at 4:47

1 Answer 1

3

To get the program's arguments, you need to define your main function with an argument count (conventionally named argc) and and argument array (conventionally named argv), so something as

int main(int argc, char**argv) {
  for (int ix=1; ix<argc; ix++) {
     FILE* fil = fopen(argv[ix], "r");
     if (!fil) { perror(argv[ix]); exit(EXIT_FAILURE); };

When you compile this (with some other needed code) into an executable foo.exe and run foo.exe a b c on the terminal, argc is 4 and you have

      argc == 4 &&
      strcmp(argv[0], "foo.exe") == 0 &&
      strcmp(argv[1], "a") == 0 &&
      strcmp(argv[2], "b") == 0 &&
      strcmp(argv[3], "c") == 0 &&
      argv[4] == NULL

Notice that it is a good habit to call perror on failure of a function like fopen

BTW, you forgot to call fclose in your program. You might learn more about fflush also. And you should prefer snprintf to sprintf to avoid buffer overflows. Learn more about, and be very scared of, undefined behavior.

Please take the habit of compiling with all warnings & debug info (e.g. gcc -Wall -Wextra -g if using GCC....) then learn how to use the debugger.

Read perror(3), fopen(3), fclose(3), fflush(3), snprintf(3) and take the habit to read the documentation of every function that you want to use.

See also csplit; you might take some inspiration by studying the source code of the free software package coreutils implementing it on Linux.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the efforts of all the people who answered me, I have completed my program...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.