讲解:C Data File Lab Assignment 1R、R

Introduction已经按照评分要求编写代码,题目要求只有读入排序,写入文件,所以把屏幕输出部分注释掉了运行方式以及比对结果的命令以及效果见截图RequirementData File Lab – Assignment 1COMP202 Revised 1 August 2017Len Hamey, This lab is the first of three assignments in COMP202.Commences: Week 1.Progress: Weeks 2, 3, 4.Due: 11:59pm, Monday Aug 28 (Week 5)Value: 15% (12% for task, 3% weekly progress)Overview of the LabData files exist in various formats. In Unix, text files are common for simple data, but large data filesare stored as binary data. In this lab, you will be developing programs to read, write, modify, andreformat the data in binary data files. The lab consists of a sequence of stages which build on eachother. The first three stages develop your skills. In the final stage you will reverse engineer a datafile using your knowledge of data representations. You have the choice between an easier finalstage (stage 4) that can earn you at most 2 marks for the stage, or a more difficult final stage (stage5) worth up to 3 marks for the stage. You may attempt both stages 4 and 5, but only the maximumof the two marks will count towards your totalThe marking outline is:Section ValueStage 1 2Stage 2 2Stage 3 2Stage 4 (max 2 marks) orstage 5 (max 3 marks)3Code style. 3Progress 3Total 15Learning OutcomesThis lab will involve you in developing the following specific skills and capabilities. Able to write programs that use C data structures, pointers and arrays. Able to read and write binary data files, and write data in text format. Able to convert between different data representations. Able to use malloc and free to construct data structures using the heap. Able to implement simple command line parameters. Able to interpret and recognise binary data representations. Research Unix library and system callsIndividual work and information resourcesThe stages of the lab are based on data files that will be provided to you. Each student will havetheir own specific data files to work with, with their own unique data format.This lab must be your own work. However, you may use resources on the Internet to obtain generalinformation including information about the C language and libraries, information about binary andtext data formats, and information about the operating system. If you obtain useful informationfrom the Internet, you must include comments at the relevant points in your code acknowledgingthe source of the information (URL) and briefly describing the key idea(s) that you are using.(Exception: information from the Unix manual pages does not require citation in your program).The Unix manual pages are available online on ash and iceberg – use the man command. You canalso find Unix manual pages online through Google. For example, to find out about the printflibrary call, use the command “man 3 printf” or Google “man printf” and to find out about thedirectory listing command ‘ls’, use the command “man ls” or Google “man ls”. However, youshould be wary of using information found online because sometimes there are differences betweendifferent Unix systems and our systems may not behave exactly the same as described in someonline documentation.The manual pages on the system (man command) are divided into sections:1. System commands such as ls, wc, etc.2. Unix system calls such as read(), open(), etc.3. Unix library such as printf(), fopen(), etc.4. Sections 4-8 contain other information.For more information on the man command, use the command “man man” to read the manualpages about the man command.Fetching your labThe lab files are accessed through the lab command which can be found at/home/unit/group/comp202/labThere is no Unix man page for the lab command (it is not a Unix system command) but there isdocumentation on iLearn and if you don’t give it any command-line parameters or options then itwill print out some brief documentation itself. This feature is common in many Unix programs. Tosee how this works, try the following command (where the $ symbol represents the Unix command-line prompt – you should type the command that is underlined in this example).$ /home/unit/group/comp202/labThe option –g is used to get a lab stage. For example, to get lab 1 stage 1, do:$ /home/unit/group/comp202/lab –g 1.1For stage 2, the option would be –g 1.2 instead. Please see the Lab Command Manual in iLearnfor more information about the lab command, including options for submitting assignments, gettingmarking reports, checking due dates and claiming your free extension days. Also, you can abbreviatethe command so that you can type “lab” instead of the full path name“/home/unit/group/comp202/lab”. For the rest of this document, we will use theabbreviated name.The lab get command downloads your lab data as a tar file. For stage 1, the tar file is stage1.tar.Tar is an archive utility (like zip) – it stores many files packed into one file. Use tar to extract thecontents of this file. You can read all about tar in the Unix man page$ man 1 tarHere is the command to extract the contents of stage1.tar.$ tar xvf stage1.tarThis will create a directory called stage1 and put the downloaded files in that directory.Submitting your lab solutionYour lab solution can be submitted using the lab command. The option –s is used to submit asolution to a lab stage. After the option, list all the files that you want to submit. Each time yousubmit, it is treated as a fresh submission, so you must list all the files that you want to submit everytime. (If you find that tedious, learn about wildcards in the bash shell.) For example:$ lab –s 1.1 stage1.c sub.c defs.hThe lab utility sends your submitted files to a server which compiles the C files together into aprogram, runs it, and tests that it works correctly for your particular lab assignment. The serverrecords information about your submission and also sends back information to you through the labcommand.You can submit as many times as you like. As a matter of personal achievement, you should aim toachieve a really good score on your initial submit, having checked that your program compileswithout errors and performs correctly on the provided sample data files. However, if there areproblems identified by the auto marker, you can resubmit without penalty.You must download each stage before you attempt to submit a solution to that stage. You shoulddownload each stage because the download provides you with the input and output data files thatyou need in order to test your program yourself.Progress marks [3 marks]Each lab assignment includes marks that are awarded for progress on the task each week. The labassignments are to be done both during lab sessions (with the assistance of lab supervisors) and inyour own time. Each week that the lab is out, you earn a progress mark if you achieve the specifiedmilestone by 11:59pm on the specified date. You can earn the progress marks early, but you cannotearn them late.If you do not achieve the milestone for a progress mark by the specified date then you lose thatweek’s progress mark and the milestone “slips” and becomes due on the next progress date. All thelater milestones also slip back by one week, but the last milestone is lost. If you achieve the slippedmilestone by the new progress date then you receive the progress mark for that date, but you havelost the progress mark for the missed date and you cannot make it up later.Milestones Monday of Week 2: Stage 1 achievement mark of at least 1.5/2.0 Monday of Week 3: Stage 2 achievement mark of at least 1.5/2.0 Monday of Week 4: Stage 3 achievement mark of at least 1.5/2.0 Monday of week 5: Lab closesDetailed marking guides for each stageWhen you extract the files for a stage you will also find a file called marking-guide.txt in theextracted files. This text file contains a detailed marking rubric for the stage. The auto marker usesthis rubric to mark your submission for the stage. The marking guide includes detailed notes thatdescribe how each mark is calculated and what is being marked.In later stages, some auto marker checks are thresholds. Threshold conditions may not earn youmarks, but are required for your program to be eligible to earn other marks. The marking report willdisplay if any threshold has failed, and it will indicate which marks are suppressed due to the failedthC代写 Data File Lab Assignment 1代写R实验、R编程作业帮做reshold. Thresholds and marks that require thresholds are indicated in the marking guidemarking-guide.txt.Stage 1: Initialising a C struct and printing it out as text [2 marks]In this stage you will declare a C data structure, create an instance of it and statically initialise it(declare it as a static or global variable and initialise it in one statement using braces). You will thenprint out the instance. This stage develops the following specific skills: Declaring a C struct. Initialising a C struct Printing various data types using printfNote: Do not use bit fields in your struct. All the data types that are specified correspond toordinary C data types.ResourcesThe following documents on iLearn may be helpful: Compile, Run, Make C Programs on Linux C Programming Notes for Data File LabYour downloaded stage1.tar file contains the following files: filestruct-description.txt: A simple description of the fields that are in yourstruct – their names and type description. initialisation-specification.txt: Specifies the initial value for each field ofyour struct. The initial value has to be formatted in a specific way in your source code – thismay mean that you have to convert one representation to another. See the lab noteDecimal, Binary, Octal and Hex. Note: It makes no difference to the data that is stored insidethe computer whether you initialise the field with decimal or the equivalent hexadecimal oroctal. However, as an exercise, we require you to make the appropriate type conversionsand the automarker will check your code. expected-output.txt: Stage 1 expected output file. Use the example in this file towork out what formatting options to use in printf.Useful Unix commandsYou might find the following Unix system commands helpful. cat diffTaskWrite a C program that declares your particular data structure as described in the C structuredescription file. Statically initialise an instance of the data structure to the initial values as specifiedin the file – use the data formats as specified in the file such as hexadecimal, decimal or octalconstants. In the main program, print out the data structure using printf formatting to make itexactly match the provided sample output file. Note that you may need to use various formattingoptions with printf to control the appearance of the output. You are expected to read aboutprintf and work out how to format the data so that it exactly matches the expected output.Submit your program for automatic assessment using the lab command. Your program style. maybe assessed according to the coding standards in the documents Some Important Comments on CodeStyle. and Systems Programming Style. which are available on iLearn.Stage 2: Reading a binary data file and printing it out [2 marks]In this stage you will read a binary data file in a known format, storing the information into instancesof a C data structure which you will then print out. This stage develops the following specific skills: Reading binary data Opening and closing files Printing various data types using printf. Using a simple command-line parameter.Resources filestruct-description.txt: Describes the members of the C data struct whichcorrespond to fields in the records of the data file. input-.bin: Sample binary input files. output-.txt: Sample text output files corresponding to the input files.Useful Unix commandsYou might find the following Unix system commands helpful. “more” or “less” diff odTaskWrite a C program that reads a file of binary data records as described in the structure descriptionfile. The program will read and print all the records in a binary data file where each record has theformat described in filestruct-description.txt. You already developed code to print outa single record in stage 1, so the focus of this stage is reading a binary data file into memory.The output formatting requirements for this stage are the same as in stage 1. However, it is possiblethat you may need to modify your record printing code – it could be that your printf call workedcorrectly for the single initialised record in stage 1 but it may not be correct for all the data recordsin the files. You should check the output against the expected output using diff, and improve yourprintf statement in whatever way is needed to get the correct output.Your program must accept one command-line parameter which is the name of the input file.The fields of the records are stored using the types specified in the data file description. The fieldsare stored packed next to each other in the data file. You cannot read the entire record directly intoa C struct in one call because C inserts additional unused space between some of the fields in thestruct (this is called alignment padding; we will discuss it later in COMP202 lectures). You mustread the data record one field at a time. It is suggested to use fread to read each field.Each record that you read should be printed out as text. Your output should exactly match thesample output files.Remember that coding style. is important: use good modularisation, and use header filesappropriately. Your program’s style. may be assessed according to the coding standards in thedocuments Some Important Comments on Code Style. and Systems Programming Style.Submit your program for marking using the lab command. We may use additional data files fortesting, including files that are larger than the samples provided to you.Stage 3: Sorting a binary data file [2 marks]In this stage you will sort files of binary data in a known format. This stage develops the followingspecific skills: Reading and writing binary data files. Opening and closing files. Working with pointers to structures. Memory allocation, dynamically sizing an array. Using system library routines (specifically, a system library sort routine). Writing code to compare structures with a lexical sort order. Using a function pointer in C.Resources filestruct-description.txt: Describes the members of the C data structure whichcorrespond to fields in the records of the data file. filestruct-sort.txt: Specifies the sorting order. input-.bin: Sample binary input files. output-.bin: Sample binary output files corresponding to the input files. The outputfiles contain the same data as the input files, but the records are sorted.Useful Unix commandsYou might find the following Unix system commands helpful. od cmpTaskModify your program from stage 2 so that it reads the input file (parameter 1), storing all the recordsinto a dynamic array in memory. The program should then sort the data records and write theoutput file (parameter 2) in sorted order.Use the Linux library sort routine qsort to perform. the sorting. Use the Unix manual (section 3) tofind out how to call the qsort library routine. Hint: you must write a comparison routine that cancompare two structures according to the sort order specified for your lab.Your program will need to store all the records in memory in order to sort them. The program willallocate a dynamic array of structs (or some other data structure), and read the data file into thearray. You do not know how large the file may be, so you must accommodate different file sizes.Here are two possible approaches (there are others).1. Dynamic sized array: Allocate an initial array of some size (e.g. 100 records) and then if(while reading the file) you find that the array is not large enough then use realloc todouble the size of it. Realloc allocates a new larger array in memory and copies the datafrom the existing array to the new larger array, before freeing the original array. Repeatedlydoubling the size allows you to accommodate arbitrarily large data files without copying thedata too many times. See the Unix manual pages for malloc and realloc.2. Compute the number of records from the file size: This is a systems approach that willrequire some reading to find out how to do it. There is a system call stat that can tell youthe total number of bytes in a file. There are also other ways to find out how many bytes arein a file but you should NOT read the entire file just to find out how big it is! Your filedescription gives you the information about how long each record is, so you can computethe number of records in the file from the number of bytes. You can then allocate an arrayof struct to the exact correct size using malloc. See the Unix manual pages for statand malloc.转自:http://ass.3daixie.com/2019030627560486.html

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,448评论 0 13
  • The Great A.I. Awakening How Google used artificial intel...
    图羽阅读 1,201评论 0 3
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,694评论 0 3
  • 这几天,我们在想:怎么才能不通过新昌江就测量出新昌江的宽度。 我们想了很久都没有想出不过江就能测出新昌江宽度的...
    你是居吧_阅读 179评论 0 0
  • 1.Feed流的前世今生 News Feed是Facebook发布的功能,3月8日凌晨消息,Facebook发布了...
    tdjgcy阅读 1,605评论 0 0