txtfile in a charcater array

This is a discussion on txtfile in a charcater array within the C Programming forums, part of the General Programming Boards category; hi Is it possible to store a file(say a txt file) in a character array??? thanks...

  1. #1
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50

    txtfile in a charcater array

    hi
    Is it possible to store a file(say a txt file) in a character array???
    thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, with some restrictions (like, you can't store more than you can possibly fit in memory, for example).

    Next you will be asking how you write code to do that, I predict.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    yeah. why didn't i ask it in the first post!!!. any link or example will be very help full
    i google it . but it didn't give me any good results.
    thanks!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, considering I could probably write the code to do that in about 10 lines (or a lot less if I write it with "compact" style), I presume you are a beginner. Since it's good for a beginner to try things out for him/herself, I suggest you make an attempt, then we can discuss the code once you have written something.

    If you are really inexperienced, start by writing something that can just open a file.

    [Yes, it probably took me more writing to answer this question than it would to search for such a solution in this forum - but you don't learn how to write code by copying someone elses code, just like you don't become a skilled author by copying another book.]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    hey matsp
    I did it . This is the only way I can think of. any way here is the code.

    the main programm
    Code:
    #include <stdio.h>
    #include "file.h";
    
    int main()
    {
    FILE *f;
    
      f=fopen("1.gif","wb");
      fwrite(&bag,sizeof(bag),1,f);
      fclose(f);
      printf("file created successfully\n");
    
    	return 0;
    }
    file.h header

    Code:
    // This header file contains the a gif file
    char bag[] =
    "\x47\x49\x46\x38\x39\x61\x40\x00\x40\x00\xF7\x00\x00\x00\x00\x00\x80\x00"
    "\x00\x00\x80\x00\x80\x80\x00\x00\x00\x80\x80\x00\x80\x00\x80\x80\x80\x80"
    "\x80\xC0\xC0\xC0\xFF\x00\x00\x00\xFF\x00\xFF\xFF\x00\x00\x00\xFF\xFF\x00"
    "\xFF\x00\xFF\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
    "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x33\x00\x00\x66\x00\x00"
    "\x99\x00\x00\xCC\x00\x00\xFF\x00\x33\x00\x00\x33\x33\x00\x33\x66\x00\x33"
    "\x99\x00\x33\xCC\x00\x33\xFF\x00\x66\x00\x00\x66\x33\x00\x66\x66\x00\x66"
    "\x99\x00\x66\xCC\x00\x66\xFF\x00\x99\x00\x00\x99\x33\x00\x99\x66\x00\x99"
    "\x99\x00\x99\xCC\x00\x99\xFF\x00\xCC\x00\x00\xCC\x33\x00\xCC\x66\x00\xCC"
    "\x99\x00\xCC\xCC\x00\xCC\xFF\x00\xFF\x00\x00\xFF\x33\x00\xFF\x66\x00\xFF"
    "\x99\x00\xFF\xCC\x00\xFF\xFF\x33\x00\x00\x33\x00\x33\x33\x00\x66\x33\x00"
    "\x99\x33\x00\xCC\x33\x00\xFF\x33\x33\x00\x33\x33\x33\x33\x33\x66\x33\x33"
    "\x99\x33\x33\xCC\x33\x33\xFF\x33\x66\x00\x33\x66\x33\x33\x66\x66\x33\x66"
    "\x99\x33\x66\xCC\x33\x66\xFF\x33\x99\x00\x33\x99\x33\x33\x99\x66\x33\x99"
    "\x99\x33\x99\xCC\x33\x99\xFF\x33\xCC\x00\x33\xCC\x33\x33\xCC\x66\x33\xCC"
    "\x99\x33\xCC\xCC\x33\xCC\xFF\x33\xFF\x00\x33\xFF\x33\x33\xFF\x66\x33\xFF"
    "\x99\x33\xFF\xCC\x33\xFF\xFF\x66\x00\x00\x66\x00\x33\x66\x00\x66\x66\x00"
    "\x99\x66\x00\xCC\x66\x00\xFF\x66\x33\x00\x66\x33\x33\x66\x33\x66\x66\x33"
    "\x99\x66\x33\xCC\x66\x33\xFF\x66\x66\x00\x66\x66\x33\x66\x66\x66\x66\x66"
    "\x99\x66\x66\xCC\x66\x66\xFF\x66\x99\x00\x66\x99\x33\x66\x99\x66\x66\x99"
    "\x99\x66\x99\xCC\x66\x99\xFF\x66\xCC\x00\x66\xCC\x33\x66\xCC\x66\x66\xCC"
    "\x99\x66\xCC\xCC\x66\xCC\xFF\x66\xFF\x00\x66\xFF\x33\x66\xFF\x66\x66\xFF"
    "\x99\x66\xFF\xCC\x66\xFF\xFF\x99\x00\x00\x99\x00\x33\x99\x00\x66\x99\x00"
    "\x99\x99\x00\xCC\x99\x00\xFF\x99\x33\x00\x99\x33\x33\x99\x33\x66\x99\x33"
    "\x99\x99\x33\xCC\x99\x33\xFF\x99\x66\x00\x99\x66\x33\x99\x66\x66\x99\x66"
    "\x99\x99\x66\xCC\x99\x66\xFF\x99\x99\x00\x99\x99\x33\x99\x99\x66\x99\x99"
    "\x99\x99\x99\xCC\x99\x99\xFF\x99\xCC\x00\x99\xCC\x33\x99\xCC\x66\x99\xCC"
    "\x99\x99\xCC\xCC\x99\xCC\xFF\x99\xFF\x00\x99\xFF\x33\x99\xFF\x66\x99\xFF"
    "\x99\x99\xFF\xCC\x99\xFF\xFF\xCC\x00\x00\xCC\x00\x33\xCC\x00\x66\xCC\x00"
    "\x99\xCC\x00\xCC\xCC\x00\xFF\xCC\x33\x00\xCC\x33\x33\xCC\x33\x66\xCC\x33"
    "\x99\xCC\x33\xCC\xCC\x33\xFF\xCC\x66\x00\xCC\x66\x33\xCC\x66\x66\xCC\x66"
    "\x99\xCC\x66\xCC\xCC\x66\xFF\xCC\x99\x00\xCC\x99\x33\xCC\x99\x66\xCC\x99"
    "\x99\xCC\x99\xCC\xCC\x99\xFF\xCC\xCC\x00\xCC\xCC\x33\xCC\xCC\x66\xCC\xCC"
    "\x99\xCC\xCC\xCC\xCC\xCC\xFF\xCC\xFF\x00\xCC\xFF\x33\xCC\xFF\x66\xCC\xFF"
    "\x99\xCC\xFF\xCC\xCC\xFF\xFF\xFF\x00\x00\xFF\x00\x33\xFF\x00\x66\xFF\x00"
    "\x99\xFF\x00\xCC\xFF\x00\xFF\xFF\x33\x00\xFF\x33\x33\xFF\x33\x66\xFF\x33"
    "\x99\xFF\x33\xCC\xFF\x33\xFF\xFF\x66\x00\xFF\x66\x33\xFF\x66\x66\xFF\x66"
    "\x99\xFF\x66\xCC\xFF\x66\xFF\xFF\x99\x00\xFF\x99\x33\xFF\x99\x66\xFF\x99"
    "\x99\xFF\x99\xCC\xFF\x99\xFF\xFF\xCC\x00\xFF\xCC\x33\xFF\xCC\x66\xFF\xCC"
    "\x99\xFF\xCC\xCC\xFF\xCC\xFF\xFF\xFF\x00\xFF\xFF\x33\xFF\xFF\x66\xFF\xFF"
    "\x99\xFF\xFF\xCC\xFF\xFF\xFF\x21\xF9\x04\x01\x00\x00\x10\x00\x2C\x00\x00"
    "\x00\x00\x40\x00\x40\x00\x00\x08\xFF\x00\xFF\x09\x1C\x48\xB0\xA0\xC1\x83"
    "\x08\x13\x2A\x5C\xC8\xB0\xA1\xC3\x87\x10\x23\x4A\x9C\x48\xB1\xA2\xC5\x8B"
    "\x18\x33\x6A\xDC\xC8\xB1\xA3\xC7\x8F\x20\x43\x8A\x1C\x49\xF2\xE2\x94\x29"
    "\x25\x31\x9E\x44\x99\xD2\xE2\x4A\x96\x2D\x25\xAE\x7C\x01\xD3\x65\xC8\x97"
    "\x35\x2B\x9E\xFC\xB8\xB2\xCF\x4E\x95\x39\x33\xAE\xF4\xE3\x33\xE8\xC4\x29"
    "\x34\x39\x9E\x7C\xE1\xD3\xCF\x4F\x93\x2B\x35\x0E\xED\xD3\xE7\xCE\x53\x97"
    "\x4B\x8D\x42\x44\xD1\x73\x8A\x55\xA7\x5A\x23\x4E\xF1\xE3\x47\xCB\x55\x88"
    "\x7E\xA8\x4C\x6D\x5A\x54\xE8\x14\x54\x4E\x69\x86\x3D\xF8\x32\xCB\xD8\x29"
    "\x3E\xBF\x9E\xA5\x38\xD6\x0F\x5C\x54\x53\xCC\xC2\x4C\x45\x98\x50\x2A\x54"
    "\xA9\x70\x9E\x24\xEB\xE7\x4E\x53\xBC\x77\xEC\x0A\xF5\x93\x8A\x32\x62\xB2"
    "\x43\x29\xA7\xA2\x46\x18\xF3\xE2\x3E\x8D\x7D\xE6\xC5\x4B\x7A\xAE\x43\xA7"
    "\x84\x09\x72\x4E\x4C\x98\x1A\xB5\x81\xFE\x56\xFB\x25\x0B\x1A\xF4\x9D\xD0"
    "\x63\xDB\x6E\x45\x51\x50\x73\xEC\x81\xAE\x29\xFB\x71\xFD\xEF\xB5\x71\x6A"
    "\xB4\x9D\x56\x55\x4E\x5A\xAF\xE9\x81\x28\xA2\x4B\x8F\x2E\x30\x15\xA1\xDF"
    "\xAE\xC7\x4E\xA3\x86\x8A\xB8\xBF\xE2\x95\x5D\x73\xFF\x27\x4A\xFE\xB6\xE3"
    "\xBB\x8D\xF7\x22\x94\xAE\x76\xCA\x74\xCA\xAE\xA7\x41\x47\x41\x78\xBB\x71"
    "\x81\x7E\x50\x68\x76\x7C\x9B\xB4\x68\xFF\xEA\x19\x14\xDD\x70\x9C\x61\x16"
    "\x5D\x6B\x9C\x0D\x94\x1F\x82\xC5\x01\xE7\x1E\x35\x84\xA0\x52\x5B\x79\x8D"
    "\x39\x45\x54\x80\x05\x71\x95\xCA\x40\x85\xDC\x75\xE0\x76\x53\x6C\x96\x98"
    "\x7E\x9B\x05\x37\xD0\x88\xC2\x85\xD6\xDF\x7F\x4D\x25\xB5\xD0\x80\xAF\x09"
    "\x44\x8D\x59\x4E\x71\xE5\x5A\x2A\x5C\x9D\x44\x5F\x89\xD4\x8C\x45\x98\x8E"
    "\xB3\x4D\x68\x9E\x85\x77\x61\x38\x9F\x16\x31\x16\xF7\x12\x8C\x23\x46\xE7"
    "\xE3\x66\xD3\xE0\xB8\x52\x74\x88\xF1\xC7\x1C\x8B\x51\x31\x34\xE0\x86\xF8"
    "\x81\xA5\x63\x88\x37\x86\xD8\x59\x2A\xDB\x4D\xE3\x9E\x7B\x1A\x26\x27\x24"
    "\x6E\x60\x39\xC4\x15\x59\xC8\x09\x26\x97\x7B\x95\xD5\xE9\x57\x1F\xA2\x68"
    "\xB6\xDA\x58\x97\x91\x07\xD9\x95\xA5\x3D\x27\xD0\x9B\x8B\x05\x96\x55\x74"
    "\x7D\xC0\x55\xD9\x5F\xB3\xD5\x47\x66\x67\xA2\x4C\x68\x1B\x9B\xA0\x19\x49"
    "\x90\x93\x2F\xD1\xC4\xD5\xA6\x8E\x91\x27\x29\x59\x76\x4A\xE8\x27\x96\xA3"
    "\xB5\xF9\xD0\x80\x8A\x6D\xCA\x55\x9E\x91\xC2\xD5\xAA\x1F\x91\xE6\xFF\xE9"
    "\xA9\x72\x9F\x8E\x65\x9E\xA5\x19\x3A\xB9\xA9\x7B\x2F\x6C\x4A\xD5\xAC\x9F"
    "\x7E\x7A\x9E\x14\x00\xFE\x37\x25\x6F\x6E\x3A\xA9\xE9\x99\x88\x02\x1B\x29"
    "\xB0\xC0\xA2\x89\x1E\x69\x60\x4D\x77\xAA\xAE\xD8\xEA\xE7\xD8\xA4\xDC\x56"
    "\xB8\xA2\x74\x4C\x38\x89\x25\x9A\xD6\x26\xBB\xAB\x74\xD0\x06\x4B\x54\x51"
    "\xD2\x85\x62\xC7\xBB\x55\x35\x65\x15\xB9\xE5\x36\x34\x1D\xBA\x6B\x76\xEB"
    "\xD5\x49\x4C\x30\x2B\x85\x1D\xA0\x10\x03\xCA\xBB\x76\xCC\xDB\xEB\xBD\xD4"
    "\x5D\x2B\xDD\x62\xB0\xAA\x09\xED\x8A\xB6\xF6\x61\x47\x28\xA0\x2C\x43\x8C"
    "\xBB\x76\x10\x8B\x70\xC2\xE6\x4A\x37\x67\x79\x58\x02\xFA\xDF\x1D\x04\x13"
    "\x43\xCC\x3B\x9A\x10\xAC\xAA\x74\xBB\x21\x9C\xC2\x49\x59\xDC\x21\x6B\xBE"
    "\xDE\x5A\xC8\x9F\xC4\x14\x5B\x5C\x8C\x1D\xB6\xD5\xDB\xF1\xBD\x67\xDA\x3A"
    "\x6A\xB1\xD4\x36\x57\x72\xC0\x7D\x74\xC6\x72\xCB\x1B\x63\x5A\xE8\xC3\x7E"
    "\x56\x2B\xC5\xBF\x13\x87\xB2\xD9\x3F\x9D\x4D\xD4\x34\xC2\x53\xBC\x8C\x53"
    "\x51\x59\x69\x3A\x1D\xBC\x04\x82\xE7\x25\xB2\x3F\x6F\x9D\xA3\x62\xC7\x6E"
    "\xCC\x84\x66\x57\x23\xB7\x59\x8D\x4C\xAB\x6D\xB7\xDA\x74\xA6\x28\x66\x62"
    "\x82\xE6\x35\x7A\xF7\xDF\x1B\x2B\x98\x18\x63\x45\x52\x04\x38\xE0\x08\x15"
    "\x3A\x25\x46\x87\xDF\xCB\xD0\xB4\x67\xC6\x24\xD6\x94\x68\x4A\x3E\x39\xB9"
    "\x96\x4B\xE4\x73\xE6\x0A\x73\xEE\xF9\xE7\xA0\x87\x2E\xFA\xE8\xA4\x97\x1E"
    "\x51\x40\x00\x3B";
    To format the above character array I wrote this utility

    Code:
    #include <stdio.h>
    
    int main()
    {
    FILE *f,*a;
    char name[10];
    char bag[1000000];
    int i=1;
    
    printf("Please enter the file name : ");
    scanf("%s",&name);
    
    
    f =fopen(name,"r");
    a=fopen("array.txt","w");
    
    while((bag[i]=fgetc(f))!=EOF)
    {
    if(i%2==0)
    {
    fprintf(a,"\\x%c%c",bag[i-1],bag[i]);
    }
    i++;	
    }
    fclose(a);
    fclose(f);
    	return 0;
    }
    the main program will extract a gif image.
    also i get this warning

    Code:
    warning C4067: unexpected tokens following preprocessor directive - expected a newline
    how to correct this warning??
    matsp is there other ways to do this????
    thanks

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    You have a ; at the end of your #include
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    thanks salem i didn't notice that.

  8. #8
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    hey guys
    I'm getting this error when i use a bigger file(about 500kb) any idea on this???

    Code:
    fatal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit
    Error executing cl.exe.
    thanks

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You current compiler settings do not allow for the largest heap size to be used. The compiler says to use the " /Zm" flag to enable a bigger heap size.

    You may need to read up on your compiler options, perhaps in a help file?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Anuradh_a View Post
    hey guys
    I'm getting this error when i use a bigger file(about 500kb) any idea on this???

    Code:
    fatal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit
    Error executing cl.exe.
    thanks
    Well, the real question would be: Why do you want to do something like this?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    thanks for the reply.
    I'm using VC++ 6.0
    when I add this /Zm Option to the project->settings->c/c++tab it says that it needs an argument

    Code:
    Command line error D2004 : '/Zm' requires an argument
    what is this argument???

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try this page:
    http://msdn.microsoft.com/en-us/libr...1c(VS.80).aspx

    It seems like the "unit" is one half-megabyte.

    I still would like to know what you are actually trying to achieve? Embedding HUGE data blocks in your application that are then made into external files seems a bit strange to me. Would it not be better to distribute the application together with the relevant file(s)?

    Edit: For Visual Studio 6, you want this page:
    http://msdn.microsoft.com/en-us/libr...80(VS.60).aspx


    --
    Mats
    Last edited by matsp; 07-10-2008 at 03:13 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    I still would like to know what you are actually trying to achieve? Embedding HUGE data blocks in your application that are then made into external files seems a bit strange to me
    Okay I know that same thing can be done using resource right. but in resources there are no size limitation to the file that stores (as far as I know). one of my friend told me that it could be done by using a character array. so I'm just checking what is the best method.
    "about the big files " I need to store a mp3 file . so when some one click on it , it will extract the mp3 and play it, so I can use this in a screen savor.

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Take a gander how NSIS does it, http://nsis.sourceforge.net/Main_Page

    It's opensource and all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21