C ++获取System的文件路径

时间:2015-12-23 01:28:51

标签: c++

我有一个JavaScript项目,需要运行节点并输出到文件,所以我决定使用C ++,我需要使用System命令。

我正在使用Visual Studio BTW

我需要链接到.exe中的另一个文件路径,以便System命令可以使用它。示例:如果我有一个包含Generator / main.js的exe,我如何获取它的路径,以便exe文件是可移植的。如何返回包含文件的路径?这可能吗?

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    cout << "Generator!";
    cin.get();
    // This needs to be run from CMD, how do I link to Generator in the project folder?
    system("node Generator/main.js > %userprofile%/Desktop/Gen.txt");
    return 0;
}

编辑如何将文件从exe文件复制到文件中,例如桌面?

1 个答案:

答案 0 :(得分:0)

虽然你的问题有些自相矛盾,但听起来你只是在与可执行文件相同的目录中查找某些内容,而不是存储在可执行文件本身中。为此,GetModuleFileName可以提供帮助。

总体思路就是这个顺序:

import java.util.*;

public class PermuteCharacters {
  char[][] rows = { 
    {},
    {'A','B','C'},
    {'D','E','F'},    
    {'G','H','I'},
    {'J','K','L'},
    {'M','N','O'},
    {'P','R','S'},
    {'T','U','V'},
    {'W','X','Y'}
  };

  StringBuffer wordBuffer = new StringBuffer();
  ArrayList<String> words = new ArrayList<String>();

  void makeWord(int currentRowIndex, int endRowIndex) {
    char[] row = rows[currentRowIndex];
    for (int i = 0; i < row.length; ++i) {
      wordBuffer.append(row[i]);
      if (currentRowIndex == endRowIndex) {
        words.add(wordBuffer.toString());
      } else {
        makeWord(currentRowIndex + 1, endRowIndex);
      }
      wordBuffer.deleteCharAt(wordBuffer.length() - 1);
    }
  }

  void makeWord(int[] indices, int position) {
    if (position == indices.length) {
      words.add(wordBuffer.toString());
      return;
    }
    char[] row = rows[indices[position]];
    for (int i = 0; i < row.length; ++i) {
      wordBuffer.append(row[i]);
      makeWord(indices, position + 1);
      wordBuffer.deleteCharAt(wordBuffer.length() - 1);
    }
  }

  void displayWords() {
    if (words.size() != 0) {
      System.out.print(words.get(0));
      for (int i = 1; i < words.size(); ++i) {
        System.out.print(" " + words.get(i));
      }
      System.out.println();
    }
    System.out.println(words.size() + " words");
  }

  public static void main(String[] args) {
    PermuteCharacters permuter = new PermuteCharacters();
    permuter.makeWord(1, 5);
    permuter.displayWords();
  }
}

如果您确实在可执行文件中存储了某些内容,则可以使用资源函数(char buffer[MAX_PATH]; GetModuleFileName(NULL, buffer, sizeof(buffer)); FindResourceLoadResource等)来访问该数据。