C中的自定义文件名

时间:2018-07-25 21:51:55

标签: c

我想问你,是否可以让用户输入FILE * ...的名称?

#region Variables
    ObservableCollection<PairPageItem> _modelPairs;
    #endregion

    #region Binding
    public ObservableCollection<PairPageItem> ModelPairs
    {
        get
        {
            return _modelPairs;
        }
        set
        {
            _modelPairs = value;
            OnPropertyChanged("ModelPairs");
        }
    }
    #endregion

    #region Constructor
    public InicioViewModel()
    {
        CreateModelPairs();
    }
    #endregion

    #region Commands
    #endregion

    #region Methods
    private void CreateModelPairs()
    {
        ObservableCollection<PairPageItem> _newModelPairs = new ObservableCollection<PairPageItem>();
        ObservableCollection<PageItem> _allModels = new ObservableCollection<PageItem>();

        _allModels.Clear();
        _allModels.Add(new PageItem { Id = 1, Title = "Promociones", TargetType = typeof(PromocionesPage), ImgSvg= "resource://Enkontrol_RubaMovil.Resources.promociones_img.svg" });
        _allModels.Add(new PageItem { Id = 2, Title = "Eventos", TargetType = typeof(EventosPage), ImgSvg = "resource://Enkontrol_RubaMovil.Resources.eventos_img.svg" });
        _allModels.Add(new PageItem { Id = 3, Title = "Comprar Casa", TargetType = typeof(ComprarCasaPage), ImgSvg = "resource://Enkontrol_RubaMovil.Resources.comprar_img.svg" });
        _allModels.Add(new PageItem { Id = 4, Title = "Seguimiento", TargetType = typeof(SeguimientoPage), ImgSvg = "resource://Enkontrol_RubaMovil.Resources.seguimiento_img.svg" });
        _allModels.Add(new PageItem { Id = 5, Title = "Contacto", TargetType = typeof(ContactoPage), ImgSvg = "resource://Enkontrol_RubaMovil.Resources.contacto_img.svg" });

        _newModelPairs.Clear();
        for (int i = 0; i < _allModels.Count; i += 2)
        {
            PageItem item1 = _allModels[i];
            PageItem item2 = i + 1 < _allModels.Count ? _allModels[i + 1] : null;

            _newModelPairs.Add(new PairPageItem(item1, item2));
        }

        ModelPairs = _newModelPairs;
    }
    #endregion

因此在控制台上将是:

#include <stdio.h>

int main() {
    char* name1, name2, path1, path2;
    printf("Insert first file name and path\nExample: file, c:\\path\\to\\file.txt");
    scanf("%s, %s", name1, path1);
    printf("Insert second file name and path\nExample: file, c:\\path\\to\\file.txt");
    scanf("%s, %s", name2, path2);
    FILE* name1;
    FILE* name2;
    name1 = fopen(path1, "a+");
    name2 = fopen(path2, "a+");
    ...
}

因此,如果用户插入:

Insert file name and path
Example: file, c:\path\to\file.txt

我希望“代码”看起来像这样:

File1, c:\file1.txt
File2, c:\file2.txt

感谢帮助;)

2 个答案:

答案 0 :(得分:4)

从流中读取的数据中不处理转义序列,因此用户应键入c:\file.txt,而不是c:\\file.txt

答案 1 :(得分:1)

您对所有字符串都有疑问

char* name1, name2, path1, path2;

name1是未初始化的指针,name2path1path2是单字符变量。

我想应该是这样的:

#define MAX_NAME 40
#define MAX_PATH 255 
char name1[MAX_NAME], name2[MAX_NAME], path1[MAX_PATH], path2[MAX_PATH];

另一个问题:

FILE* name1;
FILE* name2;

name1name2在同一作用域中声明,所以我猜您会收到“已定义”错误。

因此,如果要将名称和路径附加到FILE*上,最好的方法是使用struct,如下所示:

typedef struct File_t
{
  char name[MAX_NAME];
  char path[MAX_PATH];
  FILE* file;
};

File_t file1, file2;
scanf("%s, %s", file1.name, file1.path);
file1.file = fopen(file1.path, "a+");

scanf("%s, %s", file2.name, file2.path);
file2.file = fopen(file2.path, "a+");