Question

.com/bbcswebdav/pid-1932649-dit content-rid-6160829 1/courses/16835.201730/Lab%204.0.pdf 120 Let open a file and write to it: x-rand (8,1): fileID fopen(myfile tkt, w) will create the file fclose(fileID); Go to your workspace, look for myfile.txt and open it. What do you see? 13) How to read data from an open text file into a column vector A?Use fieanfas: A facanffleID formatspec) x 5 rand(10, 1); fileID fopen(myfile tot, n will create the file fprintf fileID, %4.4f nx): Write to fileID M- fscanf(fileID the format specifier %f is for floating point numbers fclose(fileID); Go to your workspace, look for the veetor name M and open it. What do you see? 14) How to move to a specific position in the file? Use fseek Example: copy 5 bytes from the file test dat, starting at the tenth byte, and append to the end oftest2.dat: 5% create files test1 dat and test2.d H
0 0
Add a comment Improve this question Transcribed image text
Answer #1

In both of the examples, you are supposed to run the code given and observe the output.

So, let me explain the code line by line first and then write the output.

(12) The first line is x = rand(8,1);

rand() gives the value between 0 and 1.
rand(8, 1) will give a 8-by-1 matrix i.e. 8 rows and 1 column.

Then, a file named myfile.txt is created and opened in write mode as 'w' is written.

In this file, the value of 'x' is printed using fprintf() and 4.4f means 4 digits before decimal place and 4 digits after decimal places. As the leading number is 0.. 0000.something or 0.something is same so the output I got is...

0.6792
0.3478
0.2234
0.3437
0.6652
0.1947
0.5577
0.9603

The next time I got the following output..
0.3790
0.3772
0.9652
0.0617
0.3154
0.5222
0.8289
0.7356

So, it is randomly printing 8 values to the file.

At last, it is closing the file.

(13).

In this code snippet, you are displayed how to read the values from the file.

In the first line, rand(10,1) means an array of 10-by-1 with value 0 to 1. And when multiply it with 5, it becomes the value between 0 and 4.

In the next line, mytext.txt file is opened in read mode using 'r'.

So, in the next line, fprintf() is used to write in the file but as the file is opened in the read mode, it won't write anything in this file. So, the previous values will be retained there.

In the next line, M is saving the values from the file using fscanf(). In fscanf(), the first argument is the fileID and the second argument is a format specifier in which %f is written which means all the floating point numbers are taken in the vector M.

So, as I said, the new values won't be written in the file, see the first output I wrote in (12). I am getting the same values in M which are as follows....

0.67920
0.34780
0.22340
0.34370
0.66520
0.19470
0.55770
0.96030

For the second output in (12) I got the result as :

0.37900
0.37720
0.96520
0.06170
0.31540
0.52220
0.82890
0.73560

And at the end, the file is closed.

So, these are the explanations for the questions.

Do comment if there is any query. Thank you. :)

Add a comment
Know the answer?
Add Answer to:
Let open a file and write to it: x = rand (8, 1); fileID = forpend('myfile.file.txt',...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming 6 1 point Which function(s) can be used to open a file? fread fopen...

    C Programming 6 1 point Which function(s) can be used to open a file? fread fopen fscanf fstream file.open fopens 7 1 point Which function(s), is/are used to write text to a file? fprintf printf write fseek fwrite 8 1 point Which preprocessor directive is used to make a macro? #define #ifdef #include #macro #directive 1 point You cannot write the field values of a structure variable to a file. True False 10 1 point You can use the standard...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • 1) Filename: MAE1090_HW7_1.m Write an m-file that determines the maximum value in a vector, x, and...

    1) Filename: MAE1090_HW7_1.m Write an m-file that determines the maximum value in a vector, x, and stores that maximum value as the variable y. Your m-file should work for both row and column vectors. For the purpose of testing and grading your m-file:  DO NOT issue a clear command at the start of your m-file  DO NOT define the vector x within your m-file. That vector should exist in the workspace prior to executing your m-file.  Do...

  • Write a C program which will display the contents of a file in base-16 (hexadecimal) and...

    Write a C program which will display the contents of a file in base-16 (hexadecimal) and in ASCII. Complete the following tasks: Obtain the name of the input file from the command line. If the command-line is “./hexdump xxx.bin” then argv[1] will contain “xxx.bin”. Open the file for binary input Print the entire file, 16-bytes per line. Each line should begin with an 8-digit hexadecimal offset into the file. This is the count of the bytes that you have already...

  • Select the correct answer. (1)     Which of the following will open a file named MyFile.txt and...

    Select the correct answer. (1)     Which of the following will open a file named MyFile.txt and allow you to read data from it?                (a)      File file = new File("MyFile.txt");           (b)      FileWriter inputFile = new FileWriter();           (c)       File file = new File("MyFile.txt");                      FileReader inputFile = new FileReader(file);           (d)      FileWriter inputFile = new FileWriter("MyFile.txt"); (2)     How many times will the following do - while loop be executed?                      int x = 11;             do {             x...

  • (In Linux) 1. Create a file to write to from your script and save it as...

    (In Linux) 1. Create a file to write to from your script and save it as “.txt”. 2. Write the following information to the file that you created in Step 1: Today's date Your full name Your student ID The name of your course What directory you are in File permissions of the file that you created that allow everyone to read, write, and execute The long list of files in your current directory, providing all items and their process...

  • create case 4 do Method 1:copy item by item and skip the item you want to...

    create case 4 do Method 1:copy item by item and skip the item you want to delete after you are done, delete the old file and rename the new one to the old name. #include int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1:...

  • Need this in c programming

    Question:Many files on our computers, such as executables and many music and video files, are binary files (in contrast to text files). The bytes in these files must be interpreted in ways that depend on the file format. In this exercise, we write a program data-extract to extract integers from a file and save them to an output file. The format of the binary files in this exercise is very simple. The file stores n integers (of type int). Each...

  • I am having a little trouble with my Python3 code today, I am not sure what...

    I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions: and here is my code: update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated. Lab-8 For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on...

  • This is all suppose to be a 1 file code in Matlab For the three problems...

    This is all suppose to be a 1 file code in Matlab For the three problems below create one script, HW8.m and upload it to Blackboard. Each problem must begin with the following lines of code: %Problem # (Insertproblem number) clear (clears workspace) Nothing should write to the screen except for the information requested in each problem. Le use semi-colons throughout your code. Problem 1. An industrial process of water flows through three tanks in succession as illustrated in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT