The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 write ascii file
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

hydrix

Posts

Posted - 11/17/2008 :  08:58:58 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
hi,
the origin example for creating and writing an ascii file

int file_Write_ex1()
{
file ff;
bool bOK = ff.Open("C:\\test.txt", file::modeCreate | file::modeReadWrite);
if(!bOK)
{
out_str("Failed to open file C:\test.txt");
return 0;
}
int ia = 10;
ff.Write(&ia, sizeof(ia)); //write 4 to file.
int ib[4] = {1, 2, 3, 4};
ff.Write(ib, sizeof(ib)); //write 1, 2, 3, 4 to file following previous 4.

//write double
double da = 123.345;
ff.Write(&da, sizeof(da));

//write structure
WIN32_FIND_DATAA sa;
memset(&sa, 0, sizeof(WIN32_FIND_DATAA));
sa.dwFileAttributes = 20000;
sa.ftCreationTime.dwLowDateTime = 10;
sa.ftCreationTime.dwHighDateTime = 50;
sa.ftLastAccessTime.dwLowDateTime = 100000;
sa.ftLastAccessTime.dwHighDateTime = 500000;
sa.ftLastWriteTime.dwLowDateTime = 1000;
sa.ftLastWriteTime.dwHighDateTime = 5000;
sa.nFileSizeHigh = 9000000;
sa.nFileSizeLow = 1;
sa.dwReserved0 = 600;
sa.dwReserved1 = 98745;
lstrcpy(sa.cFileName, "C:\\Origin70\\OriginC\\origin.h");
lstrcpy(sa.cAlternateFileName,"originc.h");
ff.Write(&sa, sizeof(sa));

//write char array
char ca[20] = "This is a string.";
ff.Write(ca, sizeof(ca));

//write vector
vector<int> ik = {1, 2, 3, 4, 5};
int size = sizeof(int) * ik.GetSize();
ff.Write(ik, size);

return 1;
}

compiles well. however in the resulting text file 'test.txt' i get the following data:

    Gz^@ N
2     @T  X  C:\Origin70\OriginC\origin.h originc.h This is a string.     


it seems like the numerical output is not corrrect. can anyone help me with that?
furthermore: how can i realize a line break in the resulting text file?
thanks!
hydrix

windows xp
origin 8.0

Iris_Bai

China
Posts

Posted - 11/25/2008 :  11:01:35 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

Do you want see the value of int, double in output txt file? file::Write write as binary, so cannot see them, need to convert to string and write, like:

void write_string_ex()
{
	int ia = 10;
	string str;
	str.Format("%d", ia);
	
    stdioFile ff("c:\\stdioFile_test1.txt",file::modeCreate | file::modeReadWrite);
	ff.WriteString(str);
}


About add line break, you can use stdioFile::WriteString, this method write a line to a file, end of line character is auto appended. if use file::Write, you need add "\r\n" at the end of each string.

void write_string_with_line_break_ex()
{
    //use stdioFile class, write string with line break 
    string strLine1 = "line 1:";
    string strLine3 = "line 3:";
    stdioFile ff("c:\\stdioFile_test2.txt",file::modeCreate | file::modeReadWrite);
    ff.WriteString(strLine1);
    ff.WriteString("line 2:");
    ff.WriteString(strLine3);
    
    //use file class, write string with line break 
    file ff2("c:\\file_test2.txt",file::modeCreate | file::modeReadWrite);
    strLine1 += "\r\n";
    strLine3 += "\r\n";
    string strLine2 = "line 2:\r\n";
	ff2.Write(&strLine1, sizeof(char)*strLine1.GetLength());
	ff2.Write(&strLine2, sizeof(char)*strLine2.GetLength());
	ff2.Write(&strLine3, sizeof(char)*strLine3.GetLength());
}



Edited by - Iris_Bai on 11/25/2008 11:02:27 PM
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000