1

I have a function which reads data from a file (i.e. it is passed a FILE*).

What I want to accomplish is to use that function for reading the data from a string, i.e. I would like to treat the data in the string as if it were data in a physical file (e.g. using fgets, fseek etc.), so in effect, a memory file.

I tried to associate the data string with a /dev/null (NUL) file via setvbuf (similar to what I read in this stackoverflow question), but either I did it wrong or that's not how it's done.

Can somebody help me achieving this in C, preferably in a portable fashion (actually, I don't mind using OS-specific functions/ifdefs as long as it works and it's not /too/ complicated).

Edit:

#include <stdio.h>
#include <string.h>
#define NULL_L "/dev/null"
#define NULL_W "NUL"

FILE* open_memfile(char *pc_file_as_string) {
    FILE *f = fopen(NULL_L, "rb");
    int i_size = strlen(pc_file_as_string);
    setvbuf(f, pc_file_as_string, _IOLBF, i_size);
    return f;
}

int main()
{
    char c_line[100] = "";
    char pc_file_as_string[] = "line1 asdf\nline2 fsa afds\n\nline4";
    FILE *f = open_memfile(pc_file_as_string);
    int i = 4;

    while (i > 0) {
        fgets(c_line, 100, f);
        if (c_line == NULL) 
            break;
        else
            puts(c_line);
        i--;
    }
    fclose(f);

    return 0;
}
Community
  • 1
  • 1
lineinthesand
  • 237
  • 1
  • 5
  • 15
  • So you want to read a string like we do from file ? – ameyCU Jul 29 '15 at 14:27
  • 1
    Can you show us the code you've tried so far? – larsks Jul 29 '15 at 14:29
  • I assume that writing the string in an actual file is not an option ? – n0p Jul 29 '15 at 14:29
  • 1
    Sorry, SO is no tutorial, discussion or code writing site. Please show your code or ask a specific question about a probramming problem. – too honest for this site Jul 29 '15 at 14:35
  • @Coconop yes, if possible, I'd like to avoid that. I should point out that using fmemopen works under linux, but it seems not to be available under windows, nor to have a simple winapi analog (which is why I was looking for a different solution in the first place). – lineinthesand Jul 29 '15 at 15:01
  • @larsks, I've added an example. – lineinthesand Jul 29 '15 at 15:11
  • 1
    fmemopen and mem_openstream are now in the Posix standard, so you'll find them on all recent platforms which attempt to be Posix compliant (eg Linux, OSX, *BSD). So I think your question reduces to "Is there an implementation of fmemopen for Windows", which has been asked a few times here; unfortunately, the answer today, as far as I know, is still "No". – rici Jul 29 '15 at 15:54

1 Answers1

3

On linux you would use fmemopen.

On Windows there is no fmemopen function available.

You can create and map an in memory (paging) file with the CreateFileMapping / MapViewOfFile functions, but this will return you a Windows file handle, which is unusable for your purposes.

Use sscanf and other string processing functions provided in stdio.

Konstantin W
  • 409
  • 3
  • 11