Sign in to follow this  

Saving text files using WriteFile

This topic is 4667 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a small program at the moment and have managed to save a text file. I've noticed however that it doesn't save the attributes of the text like the text colour etc and I just wondering if it was possible to save these attributes too. The code looks like:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main(int argc, char *argv[])
{
  HANDLE hIn;
  HANDLE hOut;
  
  //console stuff
  hIn = GetStdHandle(STD_INPUT_HANDLE);
  hOut = GetStdHandle(STD_OUTPUT_HANDLE);

  char string[100] = "Hello";
  //File stuff
  HANDLE hFile;
  DWORD dwBytesRead = 100;//10 * 10 * sizeof(CharInfo) structure
  DWORD dwBytesWritten;

  hFile = CreateFile("original.txt",  
        GENERIC_READ | GENERIC_WRITE,   
        0, NULL,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        
  CHAR_INFO temp[100];//storage for the saved file

  ReadFile(hFile, temp, dwBytesRead, &dwBytesWritten, NULL);
  printf("Temp is: %s\n", temp);

  //Set the text colour
  SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);

  printf("String is: %s\n", string);

  WriteFile(hFile, string, dwBytesRead, &dwBytesWritten, NULL);
  printf("%d Bytes written\n", dwBytesWritten);
  
  ReadFile(hFile, temp, dwBytesRead, &dwBytesWritten, NULL);
  printf("Temp is: %s\n", temp);

  system("PAUSE");	
  return 0;
}

When you run the program the first time the output is something like: Temp is:(text colour is white) (text colour is red) String is: Hello 100 bytes written Temp is: The second time the output is: Temp is: Hello(text colour is white) (text colour is red) String is: Hello 100 bytes written Temp is: Hello So is it possible to save the attributes of the text too and if so how?

Share this post


Link to post
Share on other sites
A plain old text file (*.txt) doesn't include formatting data like color, font, and size, since that's all a txt file is: plain old text. Either you can make you own file format or use some other format that already includes formatting in it.

That's all I can help with.

Share this post


Link to post
Share on other sites
Quote:
Original post by popcorn
How would you go about making your own file format though?


That, I don't know; though, I swear there was an article around here somewhere on how to do it but I can't find it. It was by Dino something.

I've just started C++ and have very basic knowledge of Java and research papers don't really help with the learning process...

Share this post


Link to post
Share on other sites
You could use HTML i.e. Text HERE or <span style="color:red">Text HERE</span>

Then all you have to do is open it with a web browser to see in color.

Share this post


Link to post
Share on other sites
I have found the article I think you were referring to cutthepeace but I don't think I need something as complex as that. I think if I just write the text colour out along with the text to the file then that should work. Something like the following should do I think:


#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

typedef struct textInfo
{
int colour;
char text[100];
}textInfo;

int main(int argc, char *argv[])
{
HANDLE hIn;
HANDLE hOut;

//console stuff
hIn = GetStdHandle(STD_INPUT_HANDLE);
hOut = GetStdHandle(STD_OUTPUT_HANDLE);

char string[100] = "Hello";
//File stuff
HANDLE hFile;
DWORD dwBytesRead = 110;
DWORD dwBytesWritten;

hFile = CreateFile("original.txt",
GENERIC_READ | GENERIC_WRITE,
0, NULL,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

textInfo textInfo[2];
int memForTextInfo;

memForTextInfo = sizeof(textInfo);

ReadFile(hFile, &textInfo[0], dwBytesRead, &dwBytesWritten, NULL);
SetConsoleTextAttribute(hOut, textInfo[0].colour);
printf("textInfo[0].colour is: %d\n", textInfo[0].colour);
printf("textInfo[0].text is: %s\n", textInfo[0].text);

//Set the text colour
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);

textInfo[0].colour = FOREGROUND_RED | FOREGROUND_INTENSITY;
strcpy(textInfo[0].text, string);

printf("String is: %s\n", string);

WriteFile(hFile, &textInfo[0], dwBytesRead, &dwBytesWritten, NULL);
printf("%d Bytes written\n", dwBytesWritten);
printf("textInfo[0].colour is: %d\n", textInfo[0].colour);
printf("textInfo[0].text is: %s\n", textInfo[0].text);

ReadFile(hFile, &textInfo[0], memForTextInfo, &dwBytesWritten, NULL);
printf("%d Bytes written\n", dwBytesWritten);
printf("textInfo[0].colour is: %d\n", textInfo[0].colour);
printf("textInfo[0].text is: %s\n", textInfo[0].text);

system("PAUSE");
return 0;
}



I just really added the following structure to the program

typedef struct textInfo
{
int colour;
char text[100];
}textInfo;

and then read/write into an array of these structures.

Share this post


Link to post
Share on other sites

This topic is 4667 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this