www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Writing to file problem (Kernelbase exeption)

reply "Konrad" <konradz94 o2.pl> writes:
Hi guys!
I have problem with writing to a file.
My function looks like this:
[code]
int game_change_config(string _player_spritesheet, string 
_flame_spritesheet) {
    string info = _player_spritesheet~"\n"~_flame_spritesheet;
    auto config_file = File("Data/config.ini");
    config_file.write(info);	
    return 0;
}[/code]

Everythings compile just fine, but sadly I'm getting 
Kernelbase.dll exception while function is running.
Could you help me that problem? :)

Best regards,
Ironus

PS. I'm sorry for my poor english.
Jun 05 2014
parent reply "Kagamin" <spam here.lot> writes:
Try to reduce it: remove code from the program piece by piece 
until you find a fragment, which causes the problem.
Jun 05 2014
parent reply "Konrad" <konradz94 o2.pl> writes:
Well, there is a problem with
config_file.write(info);

but when I'm changing it to
write("Data/config.ini", info);

I'm getting an error of conflict between std.stdio.write and 
std.file.write. That's why I was using the first line.

Is there any way to solve this problem?

Best regards,
Ironus
Jun 05 2014
parent reply "Stefan Koch" <uplink.coder gmail.com> writes:
config_file needs to be the string
not a struct File
.
Jun 05 2014
parent reply "Konrad" <konradz94 o2.pl> writes:
Right now I have:

import file = std.file;
import std.stdio;

int game_change_config(string _player_spritesheet, string 
_flame_spritesheet) {
    string info = _player_spritesheet~"\n"~_flame_spritesheet;
    char[] info_table;
    for(int i = 0; i < info.length; i++) {
       info_table.length++;
       info_table[info_table.length - 1] = info[i];
    }
    file.write("Data/config.ini", info_table);	
    return 0;
}

I'm changing my string into dynamic char array and I'm using 
"Data/config.ini" as a string, just as Stefan Koch suggested. 
Everything compiles and my function doesn't return any errors 
while working, but it's not changing my file content (it's still 
as same as before running a function).

Any solutions? I'm starting to get pretty angry about that 
problem (more complicated stuff is working fine and that one part 
can't)...

Best regards,
Ironus
Jun 05 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/05/2014 01:39 PM, Konrad wrote:> Right now I have:
 import file = std.file;
 import std.stdio;

 int game_change_config(string _player_spritesheet, string
 _flame_spritesheet) {
     string info = _player_spritesheet~"\n"~_flame_spritesheet;
     char[] info_table;
     for(int i = 0; i < info.length; i++) {
        info_table.length++;
        info_table[info_table.length - 1] = info[i];
That is not necessary, a simple concatenation would be the equivalent: info_table ~= info[i]; // instead of the previous two lines
     }
     file.write("Data/config.ini", info_table);
Random guess: Try with full path and see whether that helps. How about file access rights? Can you write to that file by other means? Ali
Jun 05 2014
parent "Konrad" <konradz94 o2.pl> writes:
Ok, I have rebuilded whole solution in MSVS (I'm using VisualD 
plugin) and everything started to work with my previously pasted 
code.

 Ali, thank you for advise with concatenation, I haven't thought 
about that.

Also thank you to all of you, guys, all that you wrote was very 
helpfull.

Best regards,
Ironus
Jun 05 2014