2013-09-22 38 views
2

我是Tver的編程和Iam新手。 程序有問題。我不知道在哪裏。我正在使用輸入文件和輸出文件。所以,我試圖調試程序,但我失敗了 我正在使用Visual Studio 2010. 先謝謝您。在VS 2010中調試斷言失敗fprintf

#include <iostream> 
#include <stdio.h> 
#include <math.h> 
#include <conio.h> 
#include <string.h> 
using namespace std; 
int num[100]; 
void outc(int s, int ss) 
{int i,temp,numb[100],k,l,t; 
temp=s; i=0; 
while (temp>0) 
{ 
    numb[i]=temp%ss; 
if (numb[i]>=10) numb[i]='A'-10+temp%16; 
temp/=ss; 
i++;} 
l=i/2; t=0; 
i--; 
while (i>=l) 
{ 
    k=numb[t]; 
    numb[t]=numb[i]; 
    numb[i]=k; 
    t++; 
    i--; 
} 
FILE* fooo; 
    errno_t errorCodes=fopen_s(&fooo,"output.txt","w"); 
    fprintf(fooo,"s%d= %d\n", ss, numb); 
return; 
} 
int main() 
{char c,strbuf[100],num[100]; 
char *res; 
int k,s,i,temp,ost,s2,s8,s10,s16; 


FILE* foo; 
    errno_t errorCode=fopen_s(&foo,"input.txt","r"); 
    fgets(strbuf,1000,foo); 


    if(strbuf[strlen(strbuf)-1]=='b') 
    { 
     strncpy_s(strbuf, strbuf, strlen(strbuf)-1); 
     c=atoi(strbuf); 
    k=0;s=0; 
    while(c!=0) 
    s+=(c%10)*pow(2,k); 
    c/=10; 
    k++; 
    } else 
    if(strbuf[0]==0 && strbuf[1]!='x') 
    {i=0;; 
     do{ 
     strbuf[i]=strbuf[i+1]; 
     i++; 
     }while(i!=strlen(strbuf)-1); 
     c=atoi(strbuf); 
    k=0;s=0; 
    while(c!=0) 
    s+=(c%10)*pow(8,k); 
    c/=10; 
    k++; 
    } else 
    if(strbuf[0]=='0' && strbuf[1]=='x') 
{i=0;k=strlen(strbuf); 
     do{ 

     strbuf[i]=strbuf[i+2]; 
     i++; 
     }while(i!=k); 

    puts(strbuf); 
    k=0;s=0; 
    for (i=strlen(strbuf)-1;i>=0; i--) 
    { 

    if (strbuf[i]>='A' && strbuf[i]<='F') 

    c=10+strbuf[i]-'A'; else c=strbuf[i]-'0'; 
    printf("%d\n",c); 
    s+=c*pow(16,k); 

    k++; 
    } 
    } else s=atoi(strbuf); 
    printf("%d\n",s); 
outc(s,2); 
outc(s,8); 
FILE* fooo; 
    errno_t errorCodep=fopen_s(&fooo,"output.txt","w"); 
    fprintf(fooo,"s10= %d\n", s); 
outc(s,16); 
//if (temp%16>=10) num[len-1]='A'-10+temp%16; 
//printf("s2= %d\ns8= %d\ns10= %d\ns16= %d\n", s2, s8, s, s16); 

    _getch(); 
    return 0; 
} 
+0

'FILE * fooo; errno_t errorCodes = fopen_s(&fooo,「output.txt」,「w」);'** ?? ** –

+0

@GrijeshChauhan請參閱http://msdn.microsoft.com/en-us/library/z5hh6ee9%28v=vs.90%29.aspx –

+0

您可能需要檢查該文件在使用之前是否實際打開。 –

回答

1

這有許多問題:

  1. 不能#include <iostream>或在C程序using namespace std

  2. 在這裏有很多編譯器的具體事情,這將是很難爲大多數人來幫助你。例如,你可以通過編寫標準C來讓你的生活變得更容易。例如,我無法編譯這個程序來檢查它的錯誤。

  3. 你的代碼是非常難以遵循,當你使用變量的名稱,如kss2等,和不喜歡的東西FILE * foo其次FILE * fooo。你的代碼也被格式化爲可怕的。

  4. 隨着strncpy_s(strbuf, strbuf, ...)除非微軟做的事情真的很奇怪,在這裏,你不能指定相同的字符串作爲源和目的地。

  5. strtol()atoi()更好。

  6. 你沒有關閉你打開的任何文件,而且你也沒有檢查它們是否確實打開了。在這裏使用&運營商:errorCode=fopen_s(&foo, ...是高度可疑的,但同樣,您正在使用一些非標準功能,所以誰知道。

  7. 這裏:fprintf(fooo,"s%d= %d\n", ss, numb)你告訴fprintf()期望兩個int s,但最後一個參數是一個數組。