0

Когда мы используем метод fscanf_s в Visual Studio 2013 (приложение консоли C++), Os-Winsever2008 (64 бит), указатель файла считывает данные за один или два байта заранее. Например: во время чтения текстового файла вторая строка файла «Administartor», но fscanf_s() возвращает слово как «dministrator». Пожалуйста, помогите мне исправить эту проблему. код работает нормально с Windows XP 32 бит с помощью Visual Studio 2008.fscanf_s не работает должным образом в Winserver2008 VS2013 64bit

FILE* pFile; 
pFile = NULL; 
string strFile = "E:\\10_Products.lrf"; 
fopen_s(&pFile, strFile.c_str(), "r"); 
char szTemp[256]; 
string strTemp = ""; 
if (NULL != pFile) 
{ 
    while (!feof(pFile)) 
    { 
     nRet = fscanf_s(pFile, "%s", szTemp); 
     if (EOF == nRet) 
     { 
      cout << "EOF detected"; 
     } 
    } 
    return 0; 
} 

Формат 10_Products.lrf файла приведен ниже.

[OPERATOR_LEVEL] 
Administrator 
+2

Покажите нам свой код. –

+0

Код товара \t FILE * pFile; \t pFile = NULL; \t строка strFile = "E: \\ 10_Products.lrf"; \t fopen_s (& pFile, strFile.c_str(), "r"); \t char szTemp [256]; \t string strTemp = ""; \t, если (NULL = Pfile!) \t {\t \t \t в то время (feof (Pfile)!) \t \t { \t \t \t nRet = fscanf_s (Pfile, "% s", szTemp); \t \t \t, если (EOF == nRet) \t \t \t { \t \t \t \t соиЬ << "EOF обнаружен"; \t \t \t} \t} \t возврата 0; }} –

+0

Вы должны отредактировать свое сообщение, а не добавлять его в качестве комментария –

ответ

0

Из документов на fscanf_s(), http://msdn.microsoft.com/en-us/library/6ybhk9kc.aspx:

The main difference between the secure functions (with the _s suffix) and the older functions is that the secure functions require the size of each c, C, s, S and [ type field to be passed as an argument immediately following the variable. For more information, see scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l and scanf Width Specification. 

И http://msdn.microsoft.com/en-us/library/w40768et.aspx:

Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or [. The buffer size is passed as an additional parameter immediately following the pointer to the buffer or variable. For example, if reading a string, the buffer size for that string is passed as follows: 

char s[10]; 

scanf("%9s", s, 10); 

Таким образом, вы должны назвать его так:

fscanf_s (Fp, «% 80s», cmd, sizeof (cmd));

+0

Спасибо за обновление. Он отлично работает ... Большое спасибо ... –

+0

Если бы этот ответ помог вам, можете ли вы пометить его как ответ? – CreativeMind

Смежные вопросы