Subj : Re: directly inputting numbers To : borland.public.cpp.borlandcpp From : "Michael Fruchter" Date : Thu Jul 17 2003 10:42 am Thank you for your response. Am I correct to conclude that there is no way to directly read in the text as a number? (i.e., by telling it to read a certain amount of places) Using your example, is it possible to read the file directly into the data array (without first reading the file into the buffer string)? "Ed Mulroy [TeamB]" wrote: >To convert a string to a number with atoi you have to terminate the string >with a character of value of binary zero (a value of '\0'). > >Assume that your input is a set of 7, 4-digit numbers without delimiters >that are read into a string and are destined to be placed into an array. >Here is a little program which creates a file with that kind of data then >reads it in. It does not use a comma, space or other delimiter to separate >the numbers. You didn't say if you were working with C or C++ so I wrote it >as C (but it will also work as C++). > >Try it, see that it works. It is one way to do what you asked. > >------------------------- >#include > >const char testfile_name[] = "testfile.dat"; > >int ReadTestFile(void) > { > char buffer[32]; /* deliberately made too big */ > char c; > int i; > int iposition; > int data[7]; > FILE *fin = fopen(testfile_name, "r"); > > if (fin == NULL) > { > printf("Error opening test file\n"); > return 0; > } > > fread(buffer, 7 * 4, 1, fin); > > for (i = iposition = 0; i < 7; ++i, iposition += 4) > { > /* save the next num's char */ > c = buffer[iposition + 4]; > > /* terminate the string */ > buffer[iposition + 4] = '\0'; > > /* read the number into the array */ > sscanf(buffer + iposition, "%d", &data[i]); > /* or you could have done it this way */ > /* itoa(buffer + iposition, *data[i]); */ > > /* restore the next num's char */ > buffer[iposition + 4] = c; > } > > fclose(fin); > > /* write the data that was read from the file */ > printf("Data read from the file\n"); > > for (i = 0; i < 7; ++i) > { > printf("Item %d %d\n", i, data[i]); > } > > return 1; > } > >int CreateTestFile(void) > { > const char test_data[] = "1111222233334444555566667777"; > > FILE *fout = fopen(testfile_name, "w"); > > if (fout != NULL) > { > fwrite(test_data, 28, 1, fout); > fclose(fout); > return 1; > } > > return 0; > } > >int main() > { > if (!CreateTestFile()) > { > printf("Error creating test file\n"); > return 1; > } > > ReadTestFile(); > return 0; > } >------------------------- >.. >You could have converted it in other ways. For instance, this will convert >4 character digits into a number. > > >------------------------- > char *p = test_data; /* test_data from the program above */ > int i; > int result = 0; > > for (i = 0; i < 4; ++i) > { > result = (result * 10) + (test_data[i] - '0'); > } >------------------------- > >.. Ed > >> Michael Fruchter wrote in message >> news:3f15bd8e$1@newsgroups.borland.com... >> >> Is there a way to directly input a number from a non-delimited >> text as opposed to reading in a certain amount of characters >> using "fgets" and converting to an integer using "atoi" (and >> then, if necessary, dividing by 10^x for x decimal places)? > > .