Question

In: Computer Science

how to use the filename to be passed to the given function in c ? for...

how to use the filename to be passed to the given function in c ? for example the implementation accepts one command line argument, which is a filename. The filename is a file containing a starting state to be passed to the given function initial() ,which will produce the initial state of the game.

Solutions

Expert Solution

Try this one

int main(int argc, char **argv)
{

if (argc >= 2)
  
printf("\nGame state is: %s",initial(argv[1]));
return 0;
}

/*******************Test Code*********************/

#include <stdio.h>
#include<stdlib.h>
char* initial(char *filename);
int main(int argc, char **argv)
{

if (argc >= 2)
  
printf("\nGame state is: %s",initial(argv[1]));
return 0;
}

char* initial(char *filename){
  
   char c[1000];
FILE *fptr;
if ((fptr = fopen(filename, "r")) == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);   
}
// reads text until newline
fscanf(fptr,"%[^\n]", c);
printf("Data from the file: %s", c);
return c;
fclose(fptr);
}

/*************output***************/

Data from the file: Initial State
Game state is: Initial State
--------------------------------

game.txt is passed as command line parameter having one line

Initial State


Related Solutions

write a program in C Write a function that is passed an array of characters containing...
write a program in C Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use...
c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the travellingpointer(1stversion) notation to traverse the array. The function has the following prototype. int maximum ( int *p [ ], int n); 2. Implement the function psum( )that is passed an array of n floats and returns a pointer to the sum of such an array. Print the...
20) For the given cost function C(x)=22500+800x+x2, First, find the average cost function. Use it to...
20) For the given cost function C(x)=22500+800x+x2, First, find the average cost function. Use it to find: a) The production level that will minimize the average cost? 21) Given the function f(t)=(t−3)(t+7)(t−6). its f-intercept is   its t-intercepts are b) The minimal average cost?
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Amy’s utility function is U = √ C, where C is consumption and is given by...
Amy’s utility function is U = √ C, where C is consumption and is given by C = Income − Expenses. Amy’s income is $10,000 and there is a 5% chance that she will get sick, which would cost $3,600 in medical expenses. (a) (5 points) What is Amy’s expected utility if she doesn’t have insurance? (b) (5 points) What is the actuarially fair premium for a full-coverage insurance plan? What is the actuarially fair premium for an insurance plan...
Suppose that the consumption function is given by C = 600 + 0.8Y.
Suppose that the consumption function is given by C = 600 + 0.8Y. This implies that if people’s disposable income increases by $100, the consumption will rise by $________, and the saving will rise by $________.80; 4080; 20800; 400800; 200
The cost function C and the price-demand function p are given. Assume that the value of...
The cost function C and the price-demand function p are given. Assume that the value of C(x) and p(x) are in dollars. Complete the following. C(x) = x2 100 + 7x + 2000; p(x) = − x 40 + 5 (a) Determine the revenue function R and the profit function P. R(x) = P(x) = (b) Determine the marginal cost function MC and the marginal profit function MP. MC(x) = MP(x) = Here is a picture of the problem: https://gyazo.com/6ce694b737f7dd4cfb20fbb9d1917420
Congress just passed close to a $500 billion increase in government spending:     C. How will...
Congress just passed close to a $500 billion increase in government spending:     C. How will this impact C, AD, Prices and inflation, output, and unemployment? Make sure to include an analysis using the intermediate run AD/AS model with both an intuitive and graphic analysis. (Assume that the AS supply curve is vertical like it is in the Classical Model).
Use the equation C=a+bY to motivate a discussion of the Keynesian consumption Function. Graph this function...
Use the equation C=a+bY to motivate a discussion of the Keynesian consumption Function. Graph this function and then show the effect of at least four variables on aggregate consumption expenditures. You must provide a complete explanation and diagram of each case of how each variable influences the level of consumption.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT