我是C新手,現在我試圖從文件中提取一些數字到int,但問題是雖然我可以從文件中提取它們並將它們傳遞到一個字符數組,我不能將它們轉換爲INT,我不知道爲什麼,所以這裏是代碼:從文件中提取後無法將提取的數字轉換爲int
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
#include<string.h>
char *sacardigito(char *vecchar)
{
char sodigitos[100]={},*ppsodigitos=&sodigitos,*guardarprimeiro=&sodigitos[0];
do
{
if(isdigit(*vecchar)) //if the character inside current address is a digit
{
*ppsodigitos=*vecchar; //places the digit inside the char pointer (array sodigitos)
ppsodigitos++; //increments the pointer
}
vecchar++; //increment the pointer address (array vechar)
}while(*vecchar!='\0');
printf("\nChar array in function is: %s ",guardarprimeiro); //prints the first position of the array just to make sure only numbers remain
return guardarprimeiro;
}
int main()
{
FILE *fp;
long sonumero;
int i;
char vecnumeros[100]={},*retorno;
fp=fopen("numbers.txt","r"); //open the numbers.txt file
fgets(vecnumeros,100,(FILE *)fp); //this line passes everything inside to file to a char array
printf("%s",vecnumeros); //print the char array in order to verify everything is ok
fclose(fp); //closes the file
retorno=sacardigito(vecnumeros); //sends to function to retrieve the digits
printf("\nChar array in main is: %s",retorno); //prints the retuned array
sscanf(retorno,"%ld",&sonumero); //Convert teh array to digits, I used this function from this user --> http://stackoverflow.com/questions/10204471/convert-char-array-to-a-int-number-in-c
printf("\nThe numbers in the file are the following: %ld",sonumero); //Now it gives me the error, I don't know why
return 0;
}
令我困惑的是,sscanf的不轉換數字成很長,但在這個其他代碼中,它確實(這是我僅僅嘗試從char提取數字,然後將這些數字從char轉換爲字符串的函數):
char *validacao(char *numeros)
{
char digitos[100]={},*digitospp=&digitos,*inicio=&digitos;
do
{
if(isdigit(*numeros))
{
*digitospp=*numeros;
digitospp++;
}
numeros++;
}while(*numeros!='\0');
return inicio;
}
int main()
{
char numeros[100],*retorno;
long numeroemint;
setvbuf(stdout, NULL, _IONBF, 0); // Necessario no eclipse
printf("Introduza um numero --> ");
fgets(numeros,20,stdin);
retorno=validacao(numeros);
printf("\nO vector de chars e %s",retorno);
sscanf(retorno,"%ld",&numeroemint); //esta linha transforma o vector de carcacteres numa variável tipo int
printf("\nO numero %s so com os digitos e --> %ld",numeros,numeroemint);
return 0;
}
我錯過了什麼?
親切的問候。
編輯:
所以我改變了代碼爲R薩胡提議和它的作品,但我有一個問題。另一個用戶說了一些關於未定義的行爲和自動變量和指針......有人可以澄清這一點嗎?那麼,是不是指針只是一個內存地址,我不能從函數返回一個內存地址以便以後使用?
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
#include<string.h>
long sacardigito(char *vecchar)
{
long numeroemint;
char sodigitos[100]={},*ppsodigitos=&sodigitos[0],*guardarprimeiro=&sodigitos[0];
do
{
if(isdigit(*vecchar)) //if the character inside current address is a digit
{
*ppsodigitos=*vecchar; //places the digit inside the char pointer (array sodigitos)
ppsodigitos++; //increments the pointer
}
vecchar++; //increment the pointer address (array vechar)
}while(*vecchar!='\0');
printf("\nChar array in function is: %s ",guardarprimeiro); //prints the first position of the array just to make sure only numbers remain
sscanf(guardarprimeiro, "%ld", &numeroemint);
printf("\nChar array in function after sscanf is: %ld ",numeroemint);
return numeroemint;
}
int main()
{
FILE *fp;
long retorno;
char vecnumeros[100]={};
fp=fopen("numbers.txt","r"); //open the numeros.txt file
fgets(vecnumeros,100,(FILE *)fp); //this line passes everything inside to file to a char array
printf("%s",vecnumeros); //print the char array in order to verify everything is ok
fclose(fp); //closes the file
retorno=sacardigito(vecnumeros); //sends to function to retrieve the digits
printf("\nChar array in main is: %ld",retorno); //prints the retuned array
printf("\nThe numebrs in the file are the following: %ld",retorno); //Now it gives me the error
return 0;
}
最好的問候。
返回值 - 檢查你的圖書館電話的結果。返回一個指向函數中自動變量的指針值是* undefined behavior *,這就是你用'return guardarprimeiro;'所做的事情。 'guardarprimeiro'沒用;你也可以簡單地返回'sodigitos',這同樣是未定義的行爲。 – WhozCraig
另外,爲什麼不只是'sacardigito'返回一個長? '返回atol(sodigitos);' –