2013-11-26 127 views
1

您好,我需要一個程序從文本文件 中讀取一個字符串,它的格式爲(abc12345)。我只需要閱讀 號碼,因爲我需要修改該號碼。 然後我需要再次修改編號的字符串。 Ex輸入:abcd12345輸出:abcd12346 我只知道從文件讀取整行(字符串)。 我在想,也許如果我把字符串分成兩個字符串 一個字符和另一個數字,但我不知道這樣做。從字符串中讀取數據C

這裏是我做的代碼:

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    char c[20]; 
    FILE *in,*out; 
    out=fopen("out.txt","w"); 

    if ((in=fopen("in.txt","r"))==NULL) 
    { 
     printf("Error! opening file"); 
     exit(1); 
    } 

    fscanf(in,"%20s",c); 
    printf("String that was read: %s",c); 
    fprintf(out,"%s",c); 
    fclose(in); 
    return 0; 
} 
+1

如果字符串前綴始終是一樣的,如'abcd'那麼你可以使用'sscanf的(C, 「ABCD%d」,&VAL) '改變'val',然後做'printf(「abcd%d」,val)'。 – lurker

+0

「我在想,也許如果我把字符串分成兩個字符串和一個數字」 - 好主意! 「但我不知道這麼做。」 - 通過查找字符串中的第一個十進制數字? –

+0

看看'ctype.h'。它有你可以使用的函數,如'isalpha()'和'isdigit()'。然後,只需手動瀏覽字符串,查找它從alpha變爲數字的位置,然後使用該索引來創建兩個拆分字符串。 –

回答

0

感謝答案guys.This是代碼的最終版本:
輸入文件有:
abcd123456789456789
輸出文件有:
abcd123456789456789
abcd123456789456790
abcd123456789456791
abcd123456789456792

#include <stdio.h> 
#include <stdlib.h> 

int main(){ 
char c[20]; 
char *constant; 
char *variable; 
int tmp; 

FILE *in,*out; 
in=fopen("in.txt","r"); 

if(in==NULL){ 
    printf("Error opening file"); 
    return(1);//error 
      } 
else{ 
    fgets(c,20,in); 
    } 
fclose(in); 

out=fopen("out.txt","w"); 
fprintf(out,"%s\n",c); 

variable=&c[11];//set the start position of pointer variable 
c[20]=0;//set the end position 

tmp=atoi(variable); 

int variable2,variable3,variable4; 
char buffer2[9],buffer3[9],buffer4[9]; 

variable2=tmp+1; 
variable3=tmp+2; 
variable4=tmp+3; 

itoa(variable2,buffer2,10);//in base hexa,10; 
itoa(variable3,buffer3,10); 
itoa(variable4,buffer4,10); 

constant=&c[0];//set start position of pointer constant 
c[11]=0; 



fprintf(out,"%s%s\n",constant,buffer2); 
fprintf(out,"%s%s\n",constant,buffer3); 
fprintf(out,"%s%s",constant,buffer4); 
fclose(out); 
return(0); 

} 
1

你可以這樣做:

char S[SIZE]; 
char a[SIZE]; 
int b = 0; 
char c[SIZE]; 
scanf("%s", S); 

int l = strlen(S); 
int i = 0; 
while (!isdigit(S[i])) { 
    a[i] = S[i]; 
    i++; 
} 
a[i] = '\0'; 

while (i < l) { 
    b = b * 10 + S[i] - '0'; 
    i++; 
} 

b++; 
printf("n = %d\n", b); 

sprintf(c, "%s%d", a, b); 
printf("%s\n", c); 
0
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 

char *strinc(const char *str, int d){ 
    char wk[12];//12:int need size 
    char *p; 
    int len, d_len, c; 

    c = len = strlen(str); 
    while(isdigit(str[--c])); 
    ++c; 
    d += strtol(&str[c], NULL, 10); 
    if(d<0) d = 0; 
    d_len = sprintf(wk, "%d", d); 
    p = malloc((c+d_len+1)*sizeof(char)); 
    strncpy(p, str, c); 
    p[c]='\0'; 
    return strcat(p, wk); 
} 

int main(void){//DEMO 
    char *str_num1 ="abc12345"; 
    char *incstr, *decstr; 

    incstr = strinc(str_num1, +1); 
    printf("%s\n", incstr);//abc12346 
    free(incstr); 
    incstr = strinc("abcd12345", +1); 
    printf("%s\n", incstr);//abcd12346 
    free(incstr); 
    incstr = strinc("abcd999", +1); 
    printf("%s\n", incstr);//abcd1000 
    decstr = strinc(incstr, -1); 
    printf("%s\n", decstr);//abcd999 
    free(incstr); 
    free(decstr); 

    return 0; 
}