Welcome to Laser Pointer Forums - discuss green laser pointers, blue laser pointers, and all types of lasers

LPF Donation via Stripe | LPF Donation - Other Methods

Links below open in new window

ArcticMyst Security by Avery

Coding Easy Program HELP

Joined
Dec 26, 2007
Messages
6,129
Points
0
ok,
i've been trying to write something in C

but i just cant do it

i need to write a program in C which reads the names of every file (folder or/and files) inside a folder and just save the names (not directory, dont need the extension, just the NAME) into a .txt



i.e.

if i have a folder named NIKO

and inside that folder i have:


folder HELLO
folder HI
mp3 file HELLO - YOUR SONG


the txt should have

HELLO
HI
HELLO - YOUR SONG

inside it




could you please help me to do this? i know it's simple but i forgot how to use strings and reading files in c :(
 





Joined
Oct 26, 2007
Messages
5,438
Points
83
If you can tolerate the extensions you can type:

Code:
dir /b > out.txt

However, if you're still hell-bent on using a C++ program, I made this:

Code:
#include <iostream>

#define LINE_LENGTH             300

using namespace std;

int main ()
{
    char line[LINE_LENGTH];
    while (cin)
    {
        cin.getline (line, LINE_LENGTH);
        // Read position of last period to find the location of the
        // extension:
        char *dot = strrchr(line, '.');
        if (dot)
        {
            *dot = 0; // NULL-terminate here to strip extension.
        }
        cout << line << endl;
    }
    return 0;
}

Usage (say the executable is called "strip_ext.exe"):

Code:
dir /b | strip_ext.exe > out.txt
 
Joined
Dec 26, 2007
Messages
6,129
Points
0
I appreciate your help, Bionic, but I can read C, not C++ :(


i'll try the first one :)
 
Joined
Dec 26, 2007
Messages
6,129
Points
0
Tried the 1st. one, but i dont know where the out.txt file is saved :S


EDIT: found where it is saved!

EDIT 2: damn, it doesn't go in order, and it adds a lot of stuff BEFORE the filename (the size, date & more)

isnt there a simple way to do it in C with just a string and a while (something)?
 
Joined
May 30, 2008
Messages
22
Points
0
This is a work for RECURSSION!!!! o/ (dont use this on low memory system)

Maybe, you could use fread, but i dont know where and how the file name is structured.

For sure exist some library for this.


Or you could just erase what is not the name lol. Its like shooting with a bazuka against a fly.
 
Joined
Dec 26, 2007
Messages
6,129
Points
0
NovoRei said:
This is a work for RECURSSION!!!! o/ (dont use this on low memory system)

Maybe, you could use fread, but i dont know where and how the file name is structured.

For sure exist some library for this.


Or you could just erase what is not the name lol. Its like shooting with a bazuka against a fly.


WHAT?
 
Joined
Oct 26, 2007
Messages
5,438
Points
83
C++ is superior to C and that simple program shouldn't be hard for anyone to figure out. It also doesn't seem like you're trying to learn how to use this if you're just going to use the first example, so why not just use it? It's actually only used for stripping the extension from the filename, while the actual directory traversal and listing is done by the "dir" command.

Novorei: Unless you're using an archaic 386 with 2MB of RAM or something, it's not going to matter. Besides, this can be done without the need for recursion.
 
Joined
Dec 26, 2007
Messages
6,129
Points
0
Bionic-Badger said:
C++ is superior to C and that simple program shouldn't be hard for anyone to figure out.  It also doesn't seem like you're trying to learn how to use this if you're just going to use the first example, so why not just use it?  It's actually only used for stripping the extension from the filename, while the actual directory traversal and listing is done by the "dir" command.

Novorei:  Unless you're using an archaic 386 with 2MB of RAM or something, it's not going to matter.  Besides, this can be done without the need for recursion.

Sorry if you think i meant it like that.

I need the solution, but i'd also like to learn how to code it if i ever need anything else

the thing is that i studied 1 year of C.
i cant just start guessing how to do stuff in C++

the first example actually takes me more time to revise and delete the stuff i need than copying every filename manually

i'll try to code it in C.



thanks for the help ;)
 
Joined
Dec 26, 2007
Messages
6,129
Points
0
Ok

got it

this gives me the output in a .txt temp file of the directory in which the .bat is placed:

Code:
dir /a /b /-p /o:gen >C:\WINDOWS\Temp\file_list_full.txt
start notepad C:\WINDOWS\Temp\file_list_full.txt


Thanks everyone!
 




Top