Thread: From input to output file creation

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    7

    From input to output file creation

    Hi,

    I need to write down a program which contains an input file, the program needs to write an output file which contains the content from the input file in a specific order.

    The input file contains:

    name;surname;year
    name;surname;year
    name;surname;year
    ...

    The resulting output file needs to have:

    the initials1;theinitials1DTU;year;fullname
    the initials2;theinitials2DTU;year;fullname
    the initials3;theinitials3DTU;year;fullname
    ...

    I hope some of you can give me some ideas how to write a program like this.

    Best Regards
    Urban

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Tell you what... give it your best shot, post your code and we'll see...

    Basically you have three steps to think about... loading the file, massaging the data and saving the new file...

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    Code:
    void main ()
    {
    	
    	
    	char c;
    	FILE * prt, *prt1;
    	prt = fopen("Inputfil.txt","r");
    	prt1 = fopen ("Outputfil.txt", "w");
    	while((fscanf(prt, "%c", &c))==1)						
    	{
    		printf("%c",c);
    		fflush(stdout);
    		
    		while (c!=';')										
    		{
    			fscanf(prt, "%c", &c);
    		}
    		fscanf(prt, "%c", &c);
    		printf("%c", c);
    		fflush(stdout);														
    		
    
    	
    	
    	}
    	
    	return 1;
    	
    
    	
    	
    }
    I got stuck at at this point. So far this give me the the first two initials of the first line, and thats it. So the next step would be how to add 1 to the output line and so on.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    2 things to work on for now:

    1) void main() is wrong as is return 1.

    This should be int main(void) and return 0. Read the FAQ on this site for the explanation why.

    2) You need to read a whole line from your input line. Lookup the function fgets() and how it works.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    27

    suggestion

    i am an intermediate c programmer and i am still not very confident with all my programs. in your program you need to write to another file i suppose. then somewhere there should have been a fprintf if i am not wrong. Particularly inside your while loop. something like this:
    Code:
    while(fscanf(ptr,"%c",&c)==1)/*loop shall continue untill there are characters to read*/
    {
             fprintf(ptr1,"%c",c);/*not the required code just an example*/
    }
    you will probably require to extract the necessary data and then print them into a file with some formatiing. also i didnot understand what is meant by DTU???

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by claudiu View Post
    This should be int main(void) and return 0. Read the FAQ on this site for the explanation why.
    Ummm... not to nit pick too deeply... but Main or WinMain can return any value you assign to them.. it doesnt' have to be 0. I commonly return an error value or result code to be used in batch processing with "if errorlevel" statements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input File to Output File
    By ejiroy in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2009, 11:40 PM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM