Thread: Reading/changing an inputted file

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32

    Reading/changing an inputted file

    hey all,

    i'm wondering how I would go about inserting a return and spacing/indenting into an output.

    i.e. Program reads input file, after every { it inserts a return or /n, indents all the following lines by 4 spaces, program outputs to a txt document.

    At the moment i have the following code
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    	FILE *output;
    	char line[256];
    	int x;
    	FILE * input = NULL;
    	output = fopen("output.txt", "w");
    	if((input = fopen(argv[1], "r")) != NULL )
    	{
    		while(fgets(line, 256, input) != NULL)
    		{
    			fprintf(output, "%s", line);
    			char *x = strchr(line, '{');
    			if(x)
    			{
    				printf("/n"); /*here is where i should be inserting /n
    			}
    		}
    	}
    	fclose(input);
    	fclose(output);
    }
    Where i have the printf function is where i'm stuck. If anyone could show me what to put here it would be much appreciated as i will use it try and create more rules to for format my output file.

    Thanks
    native
    Last edited by Native; 12-08-2010 at 12:08 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to insert the \n into the string BEFORE you write it to your file.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    There's no way to "insert" things (i.e. shift everything after that point to make room) into a file. You'll have to create a new file with the content you want and then, optionally of course, replace the original file with your new "fixed" file.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    				printf("/n"); /*here is where i should be inserting /n
    You want to insert newline if encounter { to output file?
    Then why are you printing to stdout??
    print to your output file!

    i.e. Program reads input file, after every { it inserts a return or /n, program outputs to a txt document.
    Your current code is still not doing this if input line has more than one {.
    Last edited by Bayint Naung; 12-08-2010 at 12:10 PM.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    Quote Originally Posted by Bayint Naung View Post
    Code:
    				printf("/n"); /*here is where i should be inserting /n
    You want to insert newline if encounter { to output file?
    Then why are you printing to stdout??
    print to your output file!


    Your current code is still not doing this.
    This is what I'm trying to do but am not sure how to go about it, any advice/tips?

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    ahha I'm not going to tell you.
    Because I'm not quite sure that the code you posted is written by you.(probably your tutor's?)
    and you are just to modify it as an assignment perhaps.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    Quote Originally Posted by Bayint Naung View Post
    I'm not quite sure that the code you posted is written by you.(probably your tutor's?)
    I wish our lecturer was that helpful, the code is based on other courseworks i have done + help from a friend + forums. Everyone's programs are meant to do the same thing our code is all fairly similar.

    Regardless, even if you don't want to post code is there anything you can hint at which might set me in the right direction? I have about 20 firefox tabs open and have been sitting in my computer lab all day trying to work it out and have gotten no-where

  8. #8
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    ok so
    Code:
     fprintf("output.txt","%c", '/n');
    is the one, but the indenting? or well it would be the one if i wasnt getting compile errors :/
    Last edited by Native; 12-08-2010 at 12:27 PM.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    New code below

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    	FILE *output;
    	char line[256];
    	int x;
    	FILE * input = NULL;
    	output = fopen("output.txt", "w");
    	if((input = fopen(argv[1], "r")) != NULL )
    	{
    		while(fgets(line, 256, input) != NULL)
    		{
    			char *x = strchr(line, '{');
    			if(x)
    			{
    				fprintf("output.txt","%c", '/n');
    			}
    		fprintf(output, "%s", line);
    		}
    	}
    	fclose(input);
    	fclose(output);
    }
    Getting the following compile error

    exL2.c:18:32: warning: multi-character character constant
    exL2.c:18:5: warning: passing argument 1 of ‘fprintf’ from incompatible pointer type /usr/include/stdio.h:335:12: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘char *’

    Not sure why this is happening, any thoughts anyone?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    To take the second part first, fprintf doesn't want a filename, it wants a FILE. You have to open the file first. (Don't worry, you can have a bunch of files open. Just open "output.txt" for "w" and go.) Also /n and \n look similar but are not in fact the same.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    '/n' isn't a newline character. You're looking for '\n'.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    Quote Originally Posted by itsme86 View Post
    '/n' isn't a newline character. You're looking for '\n'.
    Thank you! I should have spotted this, its been a long day with not enough coffee :/

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Native View Post
    I wish our lecturer was that helpful, the code is based on other courseworks i have done + help from a friend + forums. Everyone's programs are meant to do the same thing our code is all fairly similar.

    Regardless, even if you don't want to post code is there anything you can hint at which might set me in the right direction? I have about 20 firefox tabs open and have been sitting in my computer lab all day trying to work it out and have gotten no-where
    You need to think about your sequence of events...

    1) open input file.
    2) open output file
    3) read a line of text
    4) scan for the key character '{"
    5) Write out text up to that point
    6) Write the newline
    7) test for end of line
    8) if not EOL loop to step 4
    9) write the rest of the line
    8) test for end of file
    9) if not EOF loop to step 3
    10) close output file
    11) close input file


    You should be able to code that...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-11-2010, 12:05 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM