LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-30-2012, 10:47 AM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Rep: Reputation: 83
C: what's the right structure to hold a (previously unkown sized) file in memory?


Hi!

I have to read an mcrypt-encrypted file from FS and I want to keep it in memory.

The thing is that I don't know before-hand the size of the file so I don't know the size of the char * I will have to use to hold its content.

So, the structure I'm using to hold the file content is called file_content. Given that I will have to dynamically allocate the char* content I will use a char**, right?

So:

Code:
char ** file_content;
I allocate it with this:

Code:
file_content = malloc(sizeof(char) * st.st_size);
st.st_size being the size of the file from stat.h. Now, when I start reading the file I should start setting each of the chars in *file_content, right?

So:
Code:
i = 0;
char aChar;
while (i < st.st_size && fread(&aChar, 1, 1, mcrypt_input) == 1 ) {
    mdecrypt_generic (td, &aChar, 1);
    *file_content[i] = aChar;
    i++;
}
Ain't that right? Well, I know it ain't right cause I'm getting a seg-fault. So what am I missing?

Thanks in advance.
 
Old 07-30-2012, 11:03 AM   #2
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Original Poster
Rep: Reputation: 83
By the way, segfault is here:

Code:
*file_content[i] = aChar;
 
Old 07-30-2012, 11:18 AM   #3
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Original Poster
Rep: Reputation: 83
Well.... I changed file_content to char* and now it works (with the corresponding changes in the rest of the code).
 
  


Reply

Tags
char *, char **, gcc, malloc, segfault


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unkown file! Help please! voyciz Linux - Newbie 13 08-13-2010 08:19 AM
Convert directory structure from long file names in Linux to DOS 8.3 structure? manorina Linux - Software 5 09-12-2009 09:18 AM
Unkown proccess in CPU/Memory/MySQL Usage of WHM centosfan Linux - Security 4 02-19-2009 07:43 PM
File recovery from unkown embedded OS FS sprzybysz Linux - Software 1 08-17-2006 04:30 PM
Dynamically sized objects in shared memory jtshaw Programming 2 03-16-2005 10:12 AM


All times are GMT -5. The time now is 11:15 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration