Graham Aker, can you post the contents of the file that you are trying to read?
EDIT: if you haven't already done so in this long thread.
OS: Linux Mint 13(Maya) LTS 64 bit.
Aright, I removed the *, and I got an error on line 39 about an incompatable type in argument 3 of 'readnum'. How would I fix this?
Oh, yeah, the code:
EDIT: Durr, the input file. Here THAT is:Code:#include <stdlib.h> #include <stdio.h> #define big 2000 #define length 20 struct integer { int* digits; int size; }; int i, j, k, stringnum; int readnum(int stringnum, char* numbr, struct integer *emp); int main(){ FILE *ifp; char numbr[big]; struct integer num[length]; //Read in the file ifp = fopen("bigint.txt", "r"); //Scan for the number of strings fscanf(ifp, "%d", &stringnum); //Read in the numbers for (k=0; k<stringnum; k++){ fscanf(ifp, "%s", numbr); readnum(k, numbr, num[k]); } //Read the array printf("The arrays are: \n"); for (k=0; k<stringnum; k++){ for (i=0; i<j; i++) printf ("%d ", num[k].digits[i]); printf("\n"); } system("PAUSE"); return 0; }; //Define functions int readnum(int stringnum, char* numbr, struct integer *emp){ int arraysize = strlen(numbr); emp->digits = malloc(sizeof(*emp->digits) * arraysize); //Move the integer into the array for(i=arraysize, j=0; i>-1; i--){ if(isdigit(numbr[i])){ emp->digits[j++] = numbr[i] - '0'; } } emp->size = j; };
Code:2 31843694157942314379137864165 2986783975917945029875942976
Pass the address of the specific thing you want to read to the function.
This doesn't look right.
If the file had this number:Code:int stringnum;
123456
Would result in stringnum = 123456;Code:fscanf(ifp, "%d", &stringnum);
If your intention is to determine how many strings(lines) there are in the file, use fgets, which gets a string(line) at a time, then use a counter to count how many strings were read(using a loop).
OS: Linux Mint 13(Maya) LTS 64 bit.
...Uh, what? That's a bit vague...
Oh, and joy, I just found what his input file was. Posting that now...
In those three bottom lines, I'm guessing the first number is the option he wants run (1 for addition, 2 for subtraction, and 3 for comparison, based off the sample output) and the following numbers are the two strings. This actually saves me a bit of trouble along the lines of storing the data in a structure array since it's different for each option and it just prints right there, but it still means I screwed up bad and need to rewrite a decent amount of the code...Code:3 1 8888888888 2222222222 2 9999999999 10000000000 2 10000000000 9999999999
And he even gave me the funtions he wanted to use, too:
...Yeah, I've got a lot of work to do. This is what happens when you don't read ALL of the instructions... >_<Code://Preconditions: the first parameter is string that stores // only contains digits, doesn't start with // 0, and is 200 or fewer characters long. //Postconditions: The function will read the digits of the // large integer character by character, // convert them into integers and return a // pointer to the appropriate struct integer. struct integer* read_integer(char* stringInt); //Preconditions: p is a pointer to a big integer. //Postconditions: The big integer pointed to by p is // printed out. void print(struct integer *p); //Preconditions: p and q are pointers to struct integers. //Postconditions: A new struct integer is created that // stores the sum of the integers pointed to // by p and q and a pointer to it is // returned. struct integer* add(struct integer *p, struct integer *q); //Preconditions: p and q are pointers to struct integers. //Postconditions: A new struct integer is created that // stores the absolute value of the // difference between the two and a pointer // to this is returned. struct integer* subtract(struct integer *p, struct integer *q); //Preconditions: Both parameters of the function are // pointers to struct integer. //Postconditions: The function compares the digits of two // numbers and returns: // -1 if the first number is smaller than the second, // 0 if the first number is equal to the second number, // 1 if the first number is greater than the second. int compare(struct integer *p, struct integer *q);
Oh, durr. Now I feel like a complete idiot. >_<;;
Okay, so does anyone have any idea how I can take a string and turn it into an integer array with a function that's header is "struct integer* read_integer(char* stringInt);"?
Seriously? That's it?
...Please excuse me while I bang my head against this desk here.
...No, wait, hold that thought, it's crashing when I try it. Though I'm sure I'm doing it horribly wrong...
here's what I tried. Feel free to tell me how much I screwed it up:
Code:struct integer* read_integer(char* stringInt){ int arraysize = strlen(stringInt); int intarray[big]; //Move the integer into the array for(i=arraysize, j=0; i>-1; i--){ if(isdigit(stringInt[i])){ intarray[i]=stringInt[i] - '0'; } } return intarray[i]; };
horribble code
what you store in your intarray if the char is not a digit?
you return value of the int that is not initialized
you loose all you work - because the inarray is a local var that is detroyed after the function ends
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler