Open a list of files from a txt file(C) | SoloLearn: Learn to code for FREE!

0

Open a list of files from a txt file(C)

I have a txt file with a list of 3 letters like ABC DEF ... ... and i need to open all the files in the same folder that are called ABC_summary.txt,DEF_summary.txt etc... there are a few exceptions too like a file with a 4 letters name and one that is called ABC(1)_summary.txt

7/4/2020 4:18:51 PM

Guido Parlatore

7 Answers

New Answer

+1

I'm assuming that you're using Microsoft Windows due to the ABC(1)_summary.txt file. This type of file name occurs when you copy a file into a directory and a file of the same name already exists but you tell Windows to copy it anyway without overwriting the original. first of all, you need to know why you are opening the files. There are different options used for opening a file for reading, writing, appending etc. Secondly you need to decide are you going to open all of the files at the same time or one at a time. This will dictate how you manage the file handles. Next your going to need an awful lot of error handling checking every open operation and read/write operation. Finally don't forget to close all of your open file handles. If all the files in the folder you are interested in end with _summary.txt then at the command line in that folder type... dir /b /on *_summary.txt > file.lst assuming file.lst is where you want to keep your list of file names, and you will have a list of file names to open. 1. open file list, 2. while not EOF, read a line into a string. this string contains the name of the file you want to open. 3. open named file. 4. do what you gotta do. 5. close named file 6. loop back to 2. 7. close file list. 8. program ends.

+1

If they are all called xxx_summary.txt, where xxx denotes some combination of alphanumeric characters then use the dir command that I mentioned in my previous post and it will create a text file containing a list of all the file names that match the pattern. Just make sure that the filename you direct it to, in my example I used file.lst, is not used by something else because it will be overwritten If you can't be bothered to do that then just concatenate the prefix string you read from the file e.g. "ABC" with the string "_summary.txt" to generate the file name.

+1

https://code.sololearn.com/c638GT66Mr3S/#c i tried doing something like this but there must be something wrong allocating memory

+1

1. You have not supplied any header files so the program won't even compile. #include <stdio.h> #include <stdlib.h> #include <string.h> 2. Do not return 0 if an error occurs. A return of 0 means the program completed without any errors. if (lista==NULL) { printf("ERRORE ERRORE ERRORE"); return 1; }//check apertura corretta file NOTE: This will always fail in the code playground because the file lista.txt does not exist. It is also not a good idea to hard code the filename into a program. You should be taking the filename from the command line or prompting the user to enter the filename. 3. No checking of the file handle after opening the file "listan.txt". Again do not hard code the filename into the program. 4. Character pointer declared but not initialised. char* xyz = NULL; 5. Why are you using malloc to allocate one byte to nomecompleto? char* nomecompleto = malloc(sizeof(char)); 6. fgets requires a pointer to a buffer but xyz is not pointing to anything. This is fatal. If the file contains one filename per line you should be reading in one line at a time not 4 characters, this will not take into account any lines that have trailing spaces. It is better to use fscanf() and read each line into a buffer, then use sscanf to extract the stub name from the buffer. You are not checking for end of file (EOF) so this while loop will eventually cause an error. while(fgets(xyz,4,lista)!=NULL) 7. No memory to copy the strings into. nomecompleto is only one byte in size. This is fatal. nomecompleto = strcat(xyz,"_summary.txt"); 8. The syntax is correct but nomecompleto is not pointing to a valid memory area. You are not checking for write errors after writing though. fprintf(listameglio,"%s",nomecompleto); 9. you are not calling fclose() on the files that you have open at the end of the program. See: http://www.cplusplus.com/reference/cstdio/

+1

You are trying to do too much at once, especially when you have not completed the C or C++ courses. Break the problem down into small stages and build up to the full program. For the first stage just try to read in the file "lista.txt" and print it to the screen one line at a time. This will confirm that you have opened the file correctly and can read it. After you have the first stage working try to construct the full name string nomecompleto and print that to the screen. Finally when you are sure that everything is working only then write the string to the file. Don't be disheartened. File operations are tricky and pointers are difficult to understand at first. You're doing fine. Non essere scoraggiato. Le operazioni sui file sono difficili e all'inizio i puntatori sono difficili da capire. Stai facendo bene.

+1

Having seen what you are attempting to do I have come to the conclusion that what you actually require is a simple script. If the object of the exercise is to learn how to handle files in C I am more than happy to continue assisting you with your code. If, however, all you want to do is solve the immediate problem at hand then I can solve that with a single command line entry. 1. Open a command prompt window. 2. Change directory to the folder that contains your lista.txt file... cd c:\users\guido\documents use the appropriate directory name. Here I am assuming that the file is in your "My Documents" folder. 3. Enter the following line for /f %s in (lista.txt) do echo %s_summary.txt >> listan.txt that's all there is to it. 4. To view the new list use... type listan.txt NOTE: make sure that you delete listan.txt before running the command a second time because the command will add lines to the file not overwrite it. If you want to be more sophisticated you can use a batch file. If you don't know about batch files let me know and I will show you how to create a batch file to do this. It will only take a few extra lines to check if files exist before processing the data.

0

The problem is that the file with the name of the files contains only the first three letters but not the complete file name, like in the file i have "ABC" and the file i have to open is ABC_summary.txt