Thread: .txt file splitter partial program

  1. #16
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    Quote Originally Posted by tabstop View Post
    Nothing. Once you open the file for output, that's the end of that. If you want to be in a certain directory you have to open the file there.
    By this, do you mean that the file selected has to be in the folder that the output tet files are?

    Another question while I am at it. Is there a way to run the program from the command prompt in such a way that I can say type: "split.exe index_20090101.txt" and it will use the text file specified as the file name?

    Thank you.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ojm View Post
    By this, do you mean that the file selected has to be in the folder that the output tet files are?
    Not at all. I say that when you do this
    Code:
    fpTmp = fopen(TempfileName,"ab");
    TempfileName had better be what you want it to be, because you're stuck with it now.
    Quote Originally Posted by ojm View Post
    Another question while I am at it. Is there a way to run the program from the command prompt in such a way that I can say type: "split.exe index_20090101.txt" and it will use the text file specified as the file name?

    Thank you.
    Look up command line arguments.

  3. #18
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    Quote Originally Posted by tabstop View Post
    Not at all. I say that when you do this
    Code:
    fpTmp = fopen(TempfileName,"ab");
    TempfileName had better be what you want it to be, because you're stuck with it now.
    So if the directory is changed before writing to the tempory files it will work? Is there a simple line of code that can open the directory and then from then on use that directory?

    Quote Originally Posted by tabstop View Post
    Look up command line arguments.
    Thanks for that. Got the program working with the file name coming from the command line now.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ojm View Post
    So if the directory is changed before writing to the tempory files it will work? Is there a simple line of code that can open the directory and then from then on use that directory?
    On the assumption this is windows, that's _chdir from <direct.h>.

  5. #20
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    Tabstop, thank you very much for you help. I've finished my program and it works a treat. I've put the code below if anyone ever wants it in the future.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #define EXTENSION ".txt"
    int main ( int argc, char **argv )
    	{
    
     	int count=0;
    	char fileName[100];
    	char TempfileName[100];
    	char TempString[256];
    	char TempChar;
    	FILE *fp;
    	FILE *fpTmp;
    
    	fp = fopen(argv[1], "rb");
    
    	if(fp == NULL)
    		{
    		/*fprintf(stderr, "Error Opening File: %s\n", fileName); */
    		printf("Error Opening File %s: %s", argv[1], strerror(errno));
    		return -1;
    		}
    
    	else
    		printf("%s Opened Successfully.\n\n\n",argv[1]);
    
    	while(!feof(fp))
    		{
    		
    		for(count=0; ( (TempfileName[count] = fgetc(fp)) != ',' ) ; count++);
    		
    		TempfileName[count] = '\0';
    		
    		strcat(TempfileName,EXTENSION);
    		
    		chdir("split"); /* split is the sub directory, change to another if wanted */
    		
    		fpTmp = fopen(TempfileName,"ab");
    		
    		if(fpTmp == NULL)
    			{
    			fprintf(stderr, "Error Opening File: %s\n", TempfileName);
    			return -1;
    			}
    		else
    			printf("%s Opened Successfully.\n",TempfileName);
    			
    		fgets(TempString,256,fp);
    		
    		TempString[strlen(TempString)-1] = '\n';
    		
    		fputs(TempString, fpTmp);
    		
    		printf("%sWritten to %s\n", TempString, TempfileName);
    		
    		fclose(fpTmp);
    		
    		printf("%s Closed.\n\n",TempfileName);
    		
    		}
    
    	}

  6. #21
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    do not use feof to control loop - read FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 05:16 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM