Thread: Using .txt file contents as sourcecode

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    9

    Using .txt file contents as sourcecode

    ok so... Its been so long since I tried to do this that I cant seem to remember how, and I know its probably not that difficult although if someone could tell me how to get a compiler to get data from a .txt file and implement it in to the source code that would be great. I remember doing this months ago and as far as I can remember the filetype can be .txt but... meh...

    thanks a bunch! I know im asking for a good bit of tutorage but it will help me with my little project tremendously !

    P.S. this is part of a homework assignment where im only suppose to use c code... although I am allowed to ask for help on forums like these but still... blah.
    Last edited by Arcand; 07-23-2012 at 11:59 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    What's in the text file?

    A bunch of comma separated numbers like
    1, 2, 3,
    4, 5, 6


    You could do this
    Code:
    int myArray [] = {
    #include "numbers.txt"
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    within the text file is a handful of code regarding the use of the console input line as a "command line" in a quite simple text based adventure game. I dont have the code available cause I havn't written it yet, only the pseudo code, and half the point is to figure out stuff like this ( some regarding file i/o) but this problem I cant seem to find newhere...

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    If the text file is or will be actual 'c' code, use

    Code:
    #include "yourfile.txt"
    whereever you need the code. Example:

    Code:
    /* text file, file.txt */
    printf ("This is a \"text\" file that contains code.\n");
    int i;
    for (i = 0; i < 26; i++)
    {
        printf ("%c", i + 65);
    }
    printf ("\n");
    Code:
    /* main file, test.c */
    #include <stdio.h>
    
    int main ()
    {
        #include "file.txt"
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    9
    if I were to do it that way, how would I move the cursor to a different location? an example would be if I was to put four different sections of code in one file, how would I, using the #include directive, tell the program to only use the second section of code?

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Nobody here knows what you're talking about because you haven't shown us what's in these secret txt files of yours. POST SOME CODE.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Arcand View Post
    if I were to do it that way, how would I move the cursor to a different location? an example would be if I was to put four different sections of code in one file, how would I, using the #include directive, tell the program to only use the second section of code?
    It's possible to make a file generate different code for different files it's included in with macros, but to do that you need to be able to modify both the included file, and the file doing the including.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Arcand View Post
    if I were to do it that way, how would I move the cursor to a different location? an example would be if I was to put four different sections of code in one file, how would I, using the #include directive, tell the program to only use the second section of code?
    Preprocessor conditionals will work but I'm also interested in why you want to do that. Why can't you put the different sections in different files? Are you not in control of the contents of the file? Then I think including it in your code is a very bad idea.

    Bye, Andreas

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Is there a good reason why you aren't doing it the way everyone else does it - putting functions in the other file and calling them? That's the way C is designed to be used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-30-2011, 12:49 AM
  2. File I/O..Reading a file and printing contents
    By eeengenious in forum C Programming
    Replies: 2
    Last Post: 03-14-2011, 05:58 PM
  3. who wants to be a millionare TC sourcecode.
    By CarlMaroma in forum C Programming
    Replies: 1
    Last Post: 02-07-2011, 07:43 AM
  4. Error in sourcecode
    By andreas_nordman in forum C++ Programming
    Replies: 2
    Last Post: 05-30-2004, 04:45 AM
  5. Why won't the file contents clear??
    By Brown Drake in forum C++ Programming
    Replies: 5
    Last Post: 11-21-2001, 02:32 PM