How do I read from severals files in C? - c

I am working on a program which have to read from txt files.
I know that there's a function called fopen("myfile.txt","rt"), but what if I have 10 files? Do i need to call the function 10 times (a call for every file)?

Yes. But if you perform the same routines on each of those functions, abstract that behaviour into a function that accepts the name of a file. Now call that function 10 times, once with each file name.
void read_from_text_file(char const *filepath);
read_from_text_file("myfile.txt");
read_from_text_file("myfile2.txt");
...
This is a core concept in computer science. Buzzwords include "abstraction", "routine", "reusability", etc.

Yes. One function call per file is needed.

Related

Combine two C program files in VScode, macOS

I got two C program files, a basic code
main.c and function.c
the function.c file contains the function which I need to call in main.c file
i've tried merging files using -o
the error appears to be : liker not found, although all the libraries and stuff are perfectly present in my system.
I'am facing this problem in vsCode in macOS.
Let's begin with taking an example in the attachment there are two pictures if you see the picture which has code for one.c there I have used a line
#include "two.c"
Just to tell the compiler that I want to see every single definition in two. c
In file one.c I have called a function named print which is defined in two. c
One thing that can be noticed and is a really important observation is that in two.c, the file from where definitions needed to be called doesn't have a main function. Can you guess why? The reason is simply because when you click on one.c your system will call the main function located in one.c. Upon seeing two main functions on the second file it gets confused and throws error, and just think why you even need the main function in two.c. There is no need because we use the main function just to perform certain actions and in two.c you really don't need to perform any action but you only need a function to be exported on another file.
In your case, you need to eliminate the main function from the function .c file and in main .c just include the path of your function .c

C - How do I make a function that finds the location of a file it has to use just by giving it the filename? (Windows)

I am having trouble with the function fopen(). I would always send the exact location of a file as an arguement to fopen(),
which would look something like this:
fopen("c:\\Users/Username/Desktop/Projects/program_name/version 1.0/data/important_data.txt", "r");
That works just fine on my computer, but what if I decide to transfer the program to another computer?
The location would completely change.
It would no longer be:
c:\\Users/Username/Desktop/Projects/program_name/version 1.0/data/important_data.txt
But it would rather be something like:
c:\\Users/OtherUsername/Desktop/program_name/version 1.0/data/important_data.txt
My question is, how do I make a portable function which can obtain the location of a file, if I only give the
name (including the type e.g. .txt) of the file to the function?
Keep in mind, I've been learning C for less than a year. There are still a lot of things which I must learn, and
things like this are of high importance.
this is operating system specific. on linux you can use the locate shell command and parse its output ( http://www.linfo.org/locate.html )
C: Run a System Command and Get Output?
How do I execute a Shell built-in command with a C function?
however this solution will only work on linux. i think yano's solution in the comments above is better ...

Writing in two different places unformatted files in Fortran

I am trying to write in two different places (the main program and a subroutine) unformatted files in my Fortran code. The problem is that when I do it, the results change and I suspect that it is because the memory assignment is overwriting the data that I am using to make the simulation in my CFD code. I ask: Is it possible that one can just use the unformatted file (to write) once in the code? I mean, I have to use the same file to save all my data and not with different files.
I copy and past the two parts of the code to show what I am want to describe:
In the main program, the loop is:
call numcar (isave,suffix)
longueur=index(nchamp,' ')-1
nfichier=nchamp(1:longueur)//suffix
longueur=index(nfichier,' ')-1
open(10,file=nfichier(1:longueur),form='unformatted')
write(10) real(uxn,4),real(uyn,4),real(wzn,4),real(ppo,4)
close(10)
! *****************************************
isave=isave+1
and in the subroutine, the loop is:
call numcar (isavediv,suffix1)
longueur1=index(ndiv,' ')-1
nfichier1=ndiv(1:longueur1)//suffix1
longueur1=index(nfichier1,' ')-1
open(20,file=nfichier1(1:longueur1),form='unformatted')
write(20) real(ppm,4)
close(20)
! *****************************************
isavediv=isavediv+1
All the variables all declared as IMPLICIT NONE in both main program and subroutine.
I solved my problem.
The problem was that I was using the channel number 20 and a colleague of mine told me that this channel is used by the computer or some devices to process data.
I changed it for channel number 10 and it worked good again.
Thank you for your comments.
Now it looks so:
open(10,file=nfichier1(1:longueur1),form='unformatted')
write(10) real(ppm,4)
close(10)

Execute a C program from another program in gcc

I need to include a .h file to my project which will be supplied at the runtime. Since .h files are linked at linking time i am unable to include .h file. So i decided to write a dummy program which would create .h file and then i would call my actual program. Is there anyway to do this. Or any other solution is possible. I basically need to create a .h file before my program starts execution and need to link it up to my program.
i actually should take a file which is created by user, parse the file and then create a structure with the fields present in that file.for example if the file contains the following data:-
fno:int:4,fname:char:30,ftype:int:4
then i should create a structure like
struct somename
{
int fno;
char fname[30];
int ftype
};
Then i should be able to create instances of the structure created. This is what i like to do
dlopen is a solution. It allows to load dynamic library at runtime.
Compile your dummy program as a dynamic library.
Make use of dlopen on your .so
Call any function you need as if it has been linked by gcc (see dlsym).
What you can do is:
create .h file
fork
if in child: execve
if in father: wait (or not, depends on what you want to do)
I would use a Makefile; your program would receive the header file at runtime, (perhaps check it?) then execve() the make command passing the name of the file.
However, this sounds very cumbersome; perhaps you are trying to achieve something with the wrong tool. Maybe you want to use some scripting first? Or write two separate programs..? What are you trying to do?

How to remove multiple files in C using wildcards?

Is there any way in C to remove (using remove()) multiple files using a * (wildcards)?
I have a set of files that all start with Index. For example: Index1.txt, Index-39.txt etc.
They all start with Index but I don't know what text follows. There are also other files in the same directory so deleting all files won't work.
I know you can read the directory, iterate each file name, read the the first 5 chars, compare and if it fits then delete, but, is there an easier way (this is what I currently do by the way)?
This is standard C, since the code runs on Linux and Windows.
As you point out you could use diropen, dirread, dirclose to access the directory contents, a function of your own (or transform the wildcards into a regex and use a regex library) to match, and unlink to delete.
There isn't a standard way to do this easier. There are likely to be libraries, but they won't be more efficient than what you're doing. Typically a file finding function takes a callback where you provide the matching and action part of the code. All you'd be saving is the loop.
If you don't mind being platform-specific, you could use the system() call:
system("del index*.txt"); // DOS
system("rm index*.txt"); // unix
Here is some documentation on the system() call, which is part of the standard C library (cstdlib).
Is this all the program does? If so, let the command line do the wildcard expansion for you:
int main(int argc, char* argv[])
{
while (argc--)
remove(argv[argc]);
}
on Windows, you need to link against 'setargv.obj', included in the VC standard lib directory.

Resources