C ++应用程序无法在系统启动上运行. [英] c++ application not working on system start.

查看:110
本文介绍了C ++应用程序无法在系统启动上运行.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好..
这是我简单的c ++代码.


Hello..
This is my simple c++ code.


#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
    cout<<"Writing Hello World.\n";
    FILE *file;
    file=fopen("myfile.txt","a+");
    if(file!=NULL)
    {
        fputs("Hello World!\n",file);
        fclose(file);
        cout<<"Succes!\n";
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}



一切正常.应用程序创建myfile.txt(如果不存在).并写下"Hello World!"到文件.

我想在PC启动时自动启动此应用程序.因此,我手动进行了注册表设置,以便在系统启动时运行该应用程序.



This is working fine. Application creates myfile.txt(if it is not present). and writes "Hello World!" to the file.

I want to automatically launch this application when PC starts. So I manually made the registry setting to run this application on system boot.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run


应用程序在PC启动时运行.它显示控制台窗口.


Application runs when pc starts. It shows the console window.

Writing Hello World.
Succes!
Press any key to continue . . .


但问题是未创建"myfile.txt".即使该文件存在于目录中,它也不会写"Hello World!".到文件.

我不知道为什么它不创建/写入文件.
如果我手动启动该应用程序,它将起作用.

希望你们能帮助我解决这个问题.
谢谢.


but the problem is "myfile.txt" is not created. even if the file exist in the directory it does not write "Hello World!" to the file.

I dont know why it doesnt create/write file.
If i manually start the application it works.

Hope you guys will help me to solve this problem.
Thanks.

推荐答案

如果输入完整路径来调用fopen会发生什么情况?像这样的东西:

What happens if you put a full path in for the call to fopen? Something like:

FILE *file_ptr = fopen( "c:\\test.txt", "a+" );



确保路径几乎可以被任何东西写入.如果可行,那么您知道问题出在什么地方,它正在调用该程序.

干杯,



PS:使用ostreams写入控制台,然后使用该文件的C样式I/O似乎有点不可思议.尤其要考虑到您不在乎写入是否正常或文件是否正确关闭.



making sure the path is somewhere that can be written to by just about anything. If that works then you know the problem is the current working directory of whatever it is that invokes the program.

Cheers,

Ash

PS: It seems a bit weird to be using ostreams to write to the console and then fall back on C style I/O for the file. Especially considering that you don''t care if your writes work or whether the file closed properly.


我认为它是使用错误的文件夹作为应用程序的当前目录执行的.
:)
I suppose it is executed with the wrong folder as application''s current directory.
:)


感谢答案的朋友.
我不是编程专家..只是我自己学习.
如果我在编码风格上有任何错误,请原谅我.

阅读CPallini的答案后,我在c:驱动器中搜索了myfile.txt.并在system32文件夹中找到myfile.txt文件.
我感到困惑,因为我的应用程序放置在"c:\ myApplication \ HelloWorld.exe"中,但是它将文件写入了system32文件夹中.
所以我想到了在编码中获取当前目录.所以我添加了一些代码来检测当前的应用程序目录.

修改后的代码.

Thanks for the answers friends.
Im not a programming expert.. just learning by myself.
So forgive me if i made any mistakes in coding style.

After reading CPallini''s answer I did a search for myfile.txt in my c: drive. and found the myfile.txt file in system32 folder.
Im confused coz my application placed in "c:\myApplication\HelloWorld.exe" but it writes the file in system32 folder.

So i thought of getting the current directory in coding. so i added some codes to detect the current application directory.

modified code.

#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;unistd.h&gt;
using namespace std;

int main(int argc, char *argv[])
{
    
    char *path=NULL;
    size_t size;
    path=getcwd(path,size);
    cout&lt;&lt;"Current Path = "&lt;&lt;path&lt;&lt;"\n";
    
    
    
    cout&lt;&lt;"Writing Hello World.\n";
    
    FILE *file; 
    file=fopen("myfilekiruba.txt","a+"); 
    
    if(file!=NULL)
    {    
        fputs("Hello World!\n",file);
        fclose(file);
        cout&lt;&lt;"Succes!\n";
    }  
      
    system("PAUSE");
    return EXIT_SUCCESS;
}



系统重启后.应用程序启动,并显示"system32"文件夹作为当前目录.但我的应用程序未放置在system32文件夹中.而且我检查了system32文件夹中找不到我的应用程序.

这是控制台窗口的屏幕截图.
http://i780.photobucket.com/albums/yy87/kirubalks/Application.jpg [ ^ ]

如何解决这个问题.

@Aescleal ..谢谢朋友.我试过了你的建议能奏效.

但是如何解决这个问题呢?
我的应用程序放置在某处&它是从system32文件夹执行的.

希望你们能帮助我.
谢谢.



After the system reboot. Application started and it showed "system32" folder as current directory. but my application not placed in system32 folder. and also i checked in system32 folder could not find my application.

here is the screenshot of the console window.
http://i780.photobucket.com/albums/yy87/kirubalks/Application.jpg[^]

how to solve this problem.

@Aescleal.. Thanks friend. I tried your suggestion its working.

but how to solve this problem?
my application placed in somewhere & it is executing from system32 folder.

hope you guys can help me.
Thanks.


这篇关于C ++应用程序无法在系统启动上运行.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆