Hey guys,

I'm new to C and I am trying to find a way to create a file with the following array:

char peer1_0[] = {
0x0c, 0x4c, 0x08, 0x00 };

The original array has a lot more data, but I am just needing a simple example or concept that will help me in the long run. I would try searching but don't know exactly what to search for.

Thanks in advance!

Recommended Answers

All 7 Replies

Hey guys,

I'm new to C and I am trying to find a way to create a file with the following array:

char peer1_0[] = {
0x0c, 0x4c, 0x08, 0x00 };

The original array has a lot more data, but I am just needing a simple example or concept that will help me in the long run. I would try searching but don't know exactly what to search for.

Thanks in advance!

to start this could help :

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    char peer1_0[] = {0x0c, 0x4c, 0x08, 0x00 };

    ofstream out("out.txt");

    if(out.is_open())
    {
        int array_elements = sizeof(peer1_0) / sizeof(char);

        for(int i=0; i<array_elements; i++)
        {
            out << peer1_0[i] << endl;
        }
    }
    else
    {
        abort();
    }

    out.close();

    return 0;
}

jBat describes one way write data to file. You must decide what you want to write to file. The 0x notation won't appear as such in a text file using jBat's protocol. Instead what will appear is the char represented by the integer (0x...) in whatever character set (often ASCII or Unicode) is refered to by the compiler. If you want the Ox... representation of the character to appear in the file you will need a slightly different protocol, though the file writing part of what jBat wrote will (likely) remain pertinent.

to start this could help :

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    char peer1_0[] = {0x0c, 0x4c, 0x08, 0x00 };

    ofstream out("out.txt");

    if(out.is_open())
    {
        int array_elements = sizeof(peer1_0) / sizeof(char);

        for(int i=0; i<array_elements; i++)
        {
            out << peer1_0[i] << endl;
        }
    }
    else
    {
        abort();
    }

    out.close();

    return 0;
}

That looks like what I am needing, I am going to try it out and see if I can pull it off.

Thanks!

Okay, I tested it out and it works, though each element is put onto a newline, is there a way to produce them all on the same line?

Thanks

sure, leave out the call to "print" the new line char embedded in endl.

Hey guys,

I'm new to C and I am trying to find a way to create a file with the following array:

And if you are trying to learn C, you'd be better off posting questions in the C forum or you might get a little mixed up.

Hey guys,

I'm new to C and I am trying to find a way to create a file with the following array:

char peer1_0[] = {
0x0c, 0x4c, 0x08, 0x00 };

The original array has a lot more data, but I am just needing a simple example or concept that will help me in the long run. I would try searching but don't know exactly what to search for.

Thanks in advance!

You have to determine the size in bytes of your array. In your example peer1_0 is an array of char with 4 (or more) elements in it. You may determine the array size of peer1_0 through this: (sizeof(peer1_0)/sizeof(peer1_0[0])) * sizeof(char);

Either use the CreateFile or OpenFile to obtain a file handle (just make sure the file is writable. Is has a GENERIC_WRITE in CreateFile's file access flag. :-) I'm not familiar with OpenFile). For information regarding these two APIs consult the MDSN Library Documentation or MSDN on the web.

Since I'm not familiar with the OpenFile API, I'll continue with the ff API function that is used in conjunction with CreateFile, the WriteFile.

DWORD dwBytesWritten;
WriteFile(<your file handle here>, (LPCVOID)peer1_0, <the size in bytes of your array>, &dwBytesRead, NULL);

CloseHandle(<your file handle>);

I hope this would help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.