Write Midi Sequence to file - c

I Have a console program, written in C, which generates short random musical compositions using the PortMidi library. Ultimately I would like to write these sequences as either a midi or audio file.
I have found some explanations of reading and writing functions within the portmidi library: Pm_read(), and Pm_write(); but, without examples, I am struggling to understand and implement this.
Is there anyway I can export the entire sequence at once?
If not, is it necesary to recursively read into a buffer and save individual midi notes? Or do I need to read the whole sequence into the buffer and then save it?

PortMidi doesn't have any way to do do the whole thing in one shebang (as far as I can tell), so you have to manually buffer all the output MIDI messages into an array and then save them to a file. A good example of this can be found in the receive_sysex() function at http://audacity.googlecode.com/svn/audacity-src/trunk/lib-src/portmidi/pm_test/sysex.c.

Related

How to change the name of a file in C?

Currently, I am taking some measurements and saving the data in a txt file. My C program is supposed to run without interruptions and it writes several files, one after the other. On another machine I want to have a routine to copy the files that are done writing and are ready to be analysed. To distinguish between them, I want the files to be written with a tag, something like: *.part.txt.
I was thinking of using the rename() function. My problem is, I do not know how to easily change the string from *.part.txt to just *.txt.
This would be much easier using Python, but I have to use C for this one and sadly, I lack the necessary experience to solve this by myself.

How can I reuse a text file in a C program without having a file pointer?

My program has uses a text file to input data, but can't use a file pointer in the program itself. I'm supposed to use the < file.txt in the Linux terminal. I can't do it any other way because it's a college assignment, so please don't waste my time with rewind or other functions that require a pointer. I just need to be able to basically restart the text file that I already have open.
The C library provides a FILE pointer for standard input, stdin from <stdio.h>. However, it might not support all the functions you want, since it can be connected to another command (if you pipe your input from somewhere else) or the terminal (if you don't use input redirection). If you need to be able to support these, which you probably do, you won't be able to successfully call fseek or any of the related functions.
If that's the case, then this is fundamentally impossible. The computer doesn't store all the data which was sent to your program, so there is no way to go back and get it because there's nowhere to get it from. Instead, you either need to store the input yourself, or rework your algorithm to only need a single pass over the input data.

Read own source file and text file line number?

We have a assignment and the teacher doesn't go into depth with explaining things so I'm a bit confused since I haven't really done much programming before. We have to write a program that when it's done being executed it's able to read its source file and can make another text file which is the same as its source file but the text file has a line number. My problem is I don't understand how to begin it. Could someone give me an example how to get started and what steps to take? I'm not asking for someone to do the programming for me just give an example. Thanks in advance.
Roughly the steps you'll want to take are:
Read each line of the input text file
Prepend the line number to the beginning of each line.
Write your modified lines into a new text file.
There's a lot of good information on how to read/write to files here, and string concatenation (for how to prepend the line number) here. You may also want to look into for loops so that you can hit every line in the input file.
There are really two parts to your question: "Who am I?" (what file are you) and "Write a copy of myself with line numbers"
The part that you describe above is the first -- "Who am I?" and for that, something external to your source code has to provide the info because the language itself can reside in any file.
Often, there is information available about what's being compiled made available by the preprocessor (just like it sounds, it's something that is run before compiling your source code). In this case, "preprocessor macros" commonly give you this sort of environmental data.
Take a look at this link for GNU C: https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html to start researching what is available under what conditions. Your compiler, if not gcc, should have similar docs.

Reading text from a file in c

So I am trying to write a program that reads random lines of text from an input file. I can open the file but I dont know how to read characters yet, let alone character strings (only numbers so far). I am trying to make it so it can read in random lines of text and then I can manipulate them (i.e. print them in any order)
And is it possible for the program to recognize spaces (or even better periods) in between words in the input file? For example could I make it stop reading after the end of a sentence?
I am not so much looking for someone to write the code for me or anything, I am using this project as kind of a learning exercise so if anyone could tell me what topics in c to study to make this possible that would be great!
Thanks!
Reading your file with fscanf() is a good start. Have a look at the man page which will tell you how to read this either one variable at a time or many variables at a time. If your file is more free form than that, you may want to read the whole thing into memory and then process into tokens (perhaps using strtok or strtok_r), or use a combination of fscanf then process strings you have read with strtok or strtok_r. That should give you a start.

Simple C question

HI all. I am trying to make little program that reads data from file which has name of user and some data for that user. I am new on C , and how can i calculate this data for its user?line by line reading and adding each char in array? And how can I read line? is there any function?
And how can I use this each line users like object?I will make calculation for specific user.
You can use fgets to read a line at a time from the file.
Then you can parse the fields out and add them to an array or some other data structure. Just keep in mind if you use an array then you need to know ahead of time how many entries the file may contain - for example, no more than 1000. Otherwise you will need to use a data structure that can dynamically allocate memory such as a linked list, vector, etc.
Try this site, i often use it for reference.
http://www.cprogramming.com/tutorial/cfileio.html
Play around with file i/o and get use to the functions and then you will be able to make what you want.
Everything you need is in stdio.h. (Link is to a C++ website but the entire C i/o system is usable in C++; hence the documentation)

Resources