Why is this not copying?

Andy_Easton1
Andy_Easton1 used Ask the Experts™
on
I have a file called data which contains the following:-
0
0
0
0
12
0
16
0
24
0
2
0
0
0
0
0
4
3
1
8
I also have a int array called memory. I want to copy the above file into my array, but it isn't working - why? Below is my code, I use getMemory() to read into array but it doen't copy correctly.


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

typedef char * STRNG;

STRNG code[20];

int registers[8];

int memory[20];


STRNG get_line(FILE * inf)

{

STRNG s;

char tok = getc(inf);

int count = 0;

s =(char *) malloc(80);

while(tok!='\n')

{

s[count++] = tok;

tok = getc(inf);

}

s[count] = '\0';

return s;

}

void getMemory(FILE *inf) {
     STRNG line;
     int i = 0;
     while (strlen(line= get_line(inf)))
          printf ("%s\n", line);
memory[i] = (int)line;
i++;
}

void showMemory(){
     int i;
     for (i=0;i<20;i++){
          printf("%d", memory[i]);}
}




int main (int argc, char *argv[])
{
   FILE *data, *assembly;

   if ( argc < 3 )
   {
        printf("Invalid arguments");
         exit(EXIT_FAILURE);
   }

   if ( (assembly = fopen(argv[1], "r")) == NULL )
   {
       printf("Cannot open assembly file");
       exit(EXIT_FAILURE);
   }

   if ( (data= fopen(argv[2], "r")) == NULL )
   {
       printf("Cannot open data file");
       exit(EXIT_FAILURE);
   }
showMemory("/n");
getMemory(data);
showMemory();



   /* do processing */
}
Regards
Andy Easton
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
#include <string.h>

>> char tok = getc(inf);
int tok = getc(inf);

>> while(tok!='\n')
while (tok != '\n' && tok != EOF)

>> s[count++] = tok;
s[count++] = (char) tok;

>> while (strlen(line= get_line(inf)))
>>     printf ("%s\n", line);
>> memory[i] = (int)line;
>> i++;

while (strlen(line = get_line(inf)))
{
    printf ("%s\n", line);
    memory[i++] = atoi(line);
}

Commented:
Some more suggestions:
Use either fgets() or fscanf() for input instead of looping over characters.

Commented:
Also, every malloc() needs a free()
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Ok I think thats ok, but I am going to have to use the get_line function to read in my second file. My second file is assembly code as follows.
8
load   R1,R8
add   R1,16
loadind   R2,R1
add   R1,8
loadind   R3,R1
add   R3,R2
sub   R1,4
storeind   R3,R1

The first line 8 tells us how many lines are in the file.
This is a self course C guide and this is an exercise it says I have to adjust. It gives you some clues to start you of. Here is what it says should be in main. What do you think it means - do I read the assembly file into code[]?, if so how do I separete the 3 commands in each line?

int main(int argc, char* argv[])

{

int lines=0,

count = 0;


scanf("%d\n", &lines);

for(count =0;count<lines;count++) code[count]= get_line(stdin);

for(count = 0;count<lines;count++) printf("%s\n",code[count]);

return 1;

}

Author

Commented:
I take it scanf("%d\n, &lines) is to read the first line of the assembly file? -why do I need to know this. I have no clue what the next 3 lines are supposed to mean?????

Commented:
use fscanf .. if ur data file is formatted..  that means if u mean the format of the file .. where on which lines are you gonna get numbers /strings ..
say every bacth of 4 line are
string float float
string int int
int double float
float string int

so u know the pattern /format .. so you can have prewritten fscanf statements


Commented:
scanf("%d\n", &lines) reads an integer ("decimal") from the standard input (usually keyboard) and assigns it to the variable 'lines'.  I assume that is the number of lines to read.

Now, the easiest (IMO) way of dealing with the input will be to read it a line at a time using fgets(), then parse it using strtok().  The separators between the tokens will be something like " \n\t,"

See:
  http://man.he.net/man3/fgets
  http://man.he.net/man3/strtok

You can get rid of get_line()
Top Expert 2006

Commented:
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to alexo

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Sunny
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial