Thread: creating an output file

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    34

    creating an output file

    Hello,
    I am new to programming and I was wondering if someone could help me with a question? I needing to know if there is a way to write a program that will get particular lines out of a file and print the out put to another file? For example here is code that I used to get a file and output it to another file:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    #include<ctype>
    #include<mem.h>
    #define max 400

    FILE *test;
    FILE *test1;

    void main(void) {

    char word[max];
    //char word1[max]="BANANA BREAD";

    test1=fopen("c:\\output.txt","w");
    test=fopen("c:\\banana.txt","r");
    fgets(word,max,test);
    while(!feof(test)) {
    //if(memcmp(word,word1,12)==0)
    // strcat(word, "once on the lips, twice on the hips :-) ");
    fprintf(test1, "%s", word);
    fgets(word,max,test);
    }
    fclose(test);
    fclose(test1)
    }

    this actually works to my suprise! It copies a banana bread recipe to a output file. My question is this...is there a way to make the program skip the first line and the last line (for example) in the txt and print the rest to the output file? Thanks for any help on this ! I'm using Borland 4.5 if that makes any difference.

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    Skipping the first line is easy, fgets(3) will stop reading when encountering a newline, thus your first call to fgets can just discard the data/not write it out to your file.

    Skipping the last line is a little trickier. I guess you could test for eof before writing out the last line you grabbed

    Meaning, have two different buffers, so that you can read in 2 lines at a time, and when you encounter an eof, you don't write the last line to the output file

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A few pointers for you:

    >void main(void)
    main returns an int. Use
    >int main(void)
    and return something at the end of the main function.

    >test1 = fopen("c:\\output.txt", "w");
    >test = fopen("c:\\banana.txt", "r");
    Always test that the open worked (by checking that test1/test are not NULL)

    >while (!feof(test))
    You shouldn't control the read loop in this fashion, feof() isn't meant for this. Instead, use the return code from fgets().

    Now, you can "skip" the lines you don't want by simply coding an if statement around the fprintf() statement Although the simplest way to miss the first line is probably to perform a "dummy" call to fgets() to read the first line, then another to get the second line straight away. Something like
    Code:
    if (fgets(.....))  /* Reads first line */
    {
        while (fgets(....))  /* Reads the rest */
        {
            /* do some output stuff here */
        }
    }
    Knowing when you've just read in the last line is a little trickier, but I'll leave that one for you to ponder over for now
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    Thank you for the help! I more than likely be back for more. Learning a program language is discouraging at times, but when people like you all offer help and advice to green or wanna be programmers, it makes learning a bit easier! I gotta go, I have some new code to try! Thank you!

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    > ..Thank you for the help!
    > ..it makes learning a bit easier!
    No problem, come back anytime.

    Remember to use code tags when you post code next time (see my signature for an example). It's helps use see the proper formating in your code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM