我是C的新手,现在我正在尝试从文件中提取一些数字到int,但问题是虽然我可以从文件中提取它们并将它们传递给char数组,但我无法转换他们到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并没有将数字转换为long,但在其他代码中,它确实(这是我只是尝试从字母中提取数字,然后是那些来自字符串中的字符):
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 Sahu的建议更改了代码并且它有效,但我有一个问题。另一位用户说了一些关于未定义的行为和自动变量和指针的内容......有人可以澄清一下吗?那么,指针不仅仅是一个内存地址,而且我不能从函数返回一个内存地址以供以后使用吗?
#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;
}
最诚挚的问候。
答案 0 :(得分:1)
你有两个问题。首先,您将返回指向仅在函数生命周期内存在的数组的指针,从而导致未定义的行为。其次,你没有用空字符终止字符串。
答案 1 :(得分:0)
你已经从@WhozCraig留下的评论中了解了这个问题。
我建议解决问题:
将sacardigito
的返回值更改为long
long sacardigito(char *vecchar)
{
long numeroemint;
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
sscanf(guardarprimeiro, "%ld", &numeroemint);
return numeroemint;
}