C reads data from TXT and asks for help

Updated on technology 2024-05-07
3 answers
  1. Anonymous users2024-02-09

    It is read out as bytes and is converted to a number.

    #include

    #include

    int main()

    char *pchbuf = null;

    int nlen = 0;

    int num;

    file *pf = fopen("e:\\", "r");Open the file.

    fseek(pf, 0, seek_end);The file pointer is moved to the end of the file.

    nlen = ftell(pf);Get the current pointer position, which is the length of the file.

    rewind(pf);The file pointer is restored to the file header position.

    Dynamically request space, in order to save the string ending flag 0, request one more character space.

    pchbuf = (char*) malloc(sizeof(char)*nlen+1);

    if(!pchbuf)

    perror("Not enough memory! ");

    exit(0);

    Reading the content of the file There may be discrepancies between the length of the read file and the length of the source file, so the nlen is automatically adjusted here

    nlen = fread(pchbuf, sizeof(char), nlen, pf);

    pchbuf[nlen] = '\0';Add a string ending flag.

    printf("%s", pchbuf);Output the read content to the screen.

    nlen = 0;

    while(pchbuf[nlen] != '\0')

    num = 0;

    printf("lijinlindi1");

    num = num * 10 + pchbuf[nlen] -'0');

    nlen++;

    printf("result = %d",num);

    if(pchbuf[nlen] == '\0')

    break;

    nlen++;

    fclose(pf);Close the file.

    free(pchbuf);Free up space.

    return 0;

  2. Anonymous users2024-02-08

    Dude, I'm sad to read data from a file, you need to use the file read function, why don't you forget that.

  3. Anonymous users2024-02-07

    created: 2008/09/30

    created: 30:9:2008 23:29

    author: wangtk1982

    #include

    #define year 365

    #define year4 (year*4+1)

    #define year100 (year4*25-1)

    #define year400 (year100*4+1)

    long datefunc(int,int,int);

    int main(void)

    printf("Interval %ld days", datefunc(2008,10,1)- datefunc(1988,7,23));

    The days used to count the first year of the Common Era*

    long datefunc(int year,int month,int date)

    int leapyear[2][12] = {

    long days=0;Define the length of time

    Leap year symbol*

    year;*There is no 0 year A.D.*

    The cumulative number of days* for a year is calculated below

    days+=(year/400)*year400;year%=400;

    days+=(year/100)*year100;year%=100;

    days+=(year/4)*year4;year%=4;

    days+=year*year;

    The cumulative number of days in the month is calculated below*

    for (int i=0;idays+=leapyear[bleap?1:0][i];

    The cumulative number of days* for the days is calculated below

    days+=date;

    return days;*Back*

Related questions
4 answers2024-05-07

using system;

using ; >>>More

12 answers2024-05-07

This is an additive function I wrote that can judge overflow: >>>More

18 answers2024-05-07

It should be that there are a lot of contents in the system that are not fully loaded when you boot up, and you start the program, so you can't find the xml file when you boot up. You try to put the xml file in the directory where the program is located and use the file name to see it. Or you set a delay that delays the time it takes to read the XML file.

22 answers2024-05-07

I'm interested in sending it over and taking a look.

9 answers2024-05-07

I think it works, I did it once when I first started working, and I don't remember it very well, but you can try it with the seek function, locate it first, read it later, and I remember when I was doing this, I read the book The C Programmer, which is about 234 pages.