Can anyone modify my code?

AEaston
AEaston used Ask the Experts™
on
I am totally new to c and am working through various exercises to try and grasp it. I am on a course whereby you teach yourself and there are various tests etc for you to do. I am stuck on the following problem. Can anyone please help.

The program is to read in 2 files given as command line arguments via argv. The first file is a list of assembley code instruction and the second file is the initial contents of memory, which must be read into an array - basically this program is a simultor for asssembly code. Below is some code which starts as a helpful startin point. Can anyone help me? What does it mean to read the files through argv and how to I open and read the files and copy the contents into the arry. All arrys have already been declared as shown below. HOw do I modify the following code.

#include<stdlib.h>
#include<stdio.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 = malloc(80);

while(tok!='\n')

{

s[count++] = tok;

tok = getc(inf);

}

s[count] = '\0';

return s;

}


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;

}

regards
Andy Easton
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
When a program starts it's command line arguments are passed to the program using argv which is an array of character arrays.  argc contains the number of arguments passed to the program.  The first element of argv should be the program name so if a program is passed no arguments argc would still be 1 and argv[0] would contain the name of the program.

If you're asked to read files through argv then this means that the names of the files to use should be passed to the program as command line arguments ie

my_prog assembly_file data_file

in this instance argc would be 3 and argv[0] would contain "my_prog", argv[1] - "assembly_file" and argv[2] - "data_file".

to open and read the files you would use something like

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

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

    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);
    }  

    /* do your processing */
}

A couple of other points to note with the code you posted -

1) in get_line you malloc 80 bytes for each line but you do not check that you are not overflowing the end of this.  If a line is longer than 80 bytes you'll probably get a segmentation violation

2) You could use fgets to read a line of the file at a time -

fgets(buffer, sizeof(buffer), data);

This will read at most "sizeof(buffer)" bytes but will stop earlier if it encounters a new line.  If you declare buffer to be big enough you shouldn't encounter the case where a line is split across 2 reads but it is still possible.  Your get_line function could be changed to something like :-

STRNG get_line( FILE *inf)
{
    char buffer[BUFSIZ];

    if ( fgets(buffer, sizeof(buffer), inf) == NULL)
    {
         prinf("Read failed");
         return(NULL);
    }
    return(strdup(buffer));
}

Hope this helps

Author

Commented:
Ok still a bit confused. Ignore eveything above and just assume I have 2 files, what is the code I need to open these files? Do I declare what argv[0], argv[1] & argv[2] are and how do i do this? Where do I put whre thses files are located. can you please give me codee that I caqn compile that will open 1 file, close it, open the 2nd file and close it. But these must be given as command line arguments via argv. Hopefully this will make it clearer. Assume my path for the files are a:\\test1.in and a:\\test2.in.

If you can post exact code which I can compile, I'm sure it will help me understand this difficult concept.

Regards
Andy Easton  

Author

Commented:
Ok still a bit confused. Ignore eveything above and just assume I have 2 files, what is the code I need to open these files? Do I declare what argv[0], argv[1] & argv[2] are and how do i do this? Where do I put whre thses files are located. can you please give me codee that I caqn compile that will open 1 file, close it, open the 2nd file and close it. But these must be given as command line arguments via argv. Hopefully this will make it clearer. Assume my path for the files are a:\\test1.in and a:\\test2.in.

If you can post exact code which I can compile, I'm sure it will help me understand this difficult concept.

Regards
Andy Easton  
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 still a bit confused. Ignore eveything above and just assume I have 2 files, what is the code I need to open these files? Do I declare what argv[0], argv[1] & argv[2] are and how do i do this? Where do I put whre thses files are located. can you please give me codee that I caqn compile that will open 1 file, close it, open the 2nd file and close it. But these must be given as command line arguments via argv. Hopefully this will make it clearer. Assume my path for the files are a:\\test1.in and a:\\test2.in.

If you can post exact code which I can compile, I'm sure it will help me understand this difficult concept.

Regards
Andy Easton  
argv is populated automatically. You don't have to do anything to it.  argc is set to the number of "entries" in argv.

To open the files using a FILE* use fopen().

ex.
int main (int argc, char**argv)
{
    FILE *infile1;

    infile1 = fopen (argv [1], "r");

Sorry, hit enter. Here is the example:

#include <stdio.h>

int
main (int argc, char **argv)
{
    FILE* file1;

    file1 = fopen (argv [1], "r");
    /* Do stuff here.... */
    fclose (file1);
    return (0);
}
Commented:
Nearly getting there - where do I specify the path of the file. argv[1] is the file, but where do I specify the path?

argv[1] = a:\\test1.txt
argv[2] = a:\\test2.txt

?

Commented:
Let's say you compile and link your program and the resulting executable is MYPROG.EXE

You run it as following:
    MYPROG a:\test1.txt a:\test2.txt

So, if your main() is declared thus:

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

It will automagically set the arguments as follows:

    argc == 3
    argv[0] == "MYPROG.EXE (or the full path to the program)
    argv[1] == "a:\\test1.txt"
    argv[2] == "a:\\test2.txt"
    argv[3] == NULL
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 and 175:50:50 split between garethwebbley , DavidFilion and 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