Making 2000 img tags :(

p4p1 [lexostras]
9 years ago

0

Hy guys so I am working on making a website but i have 2000 images to add. So after a big reflection(not really), I thought about making all of them at once with some kind of program. I am learning C right now and i thought i would make that program to do a bit of exercise. So yeah I looked onine and found some code to list out an full directory, so i took the code and added something to print all of the file names to a directory like this:

#include <stdlib.h>  
#include <sys/types.h>  
#include <dirent.h>  

int main (void)  
{  
  FILE *f = fopen("/home/lexostras/Documents/c-ex/1Extra/filelister/text.txt", "w");  
  DIR *dp;  
  struct dirent *ep;  

  dp = opendir ("./");  
  if (f == NULL) exit(3);  
  if (dp != NULL)  
    {  
      while (ep = readdir (dp))  

           puts (ep->d_name);  
        fprintf(f, "%s",ep->d_name);  
           closedir (dp);  

    }  
  else  
    perror ("Couldn't open the directory");  

  fclose(f);  
  printf("dine");  

  return 0;  
}```[/spoiler]  
After writing that I compiled it with no errors thinking its gonna be fine so i run it and I get this:  
[spoiler]  

[lexostras | mar. sept. 22 | 0 | 42 | ~/Documents/c-ex/1Extra/filelister]
Lexostras@Leo:~$ ./lister
lister
..
text.txt
.
lister.c
Segmentation fault (core dumped)
```

I googled the Segmentation part and i think it is because I am sending data to a pointer that is non existant. BTW the file did not get edited so i think i have an error with my file, the path for the file is correct so I don’t know what is my error please help.

6replies
4voices
209views
singleton
9 years ago

0

Although indenting the code in the while, you didn’t surround it with curly brackets.
So the only statement executed in the while loop is the puts.
Once all the files in the directory are listed with puts, ep is set to null.
Then you try to fprintf with ep->d_name, but ed is null and so you de-reference a null and get segmentation fault.

I’m not sure what you’re trying to do, though… You can use bash or something instead.

p4p1 [lexostras]
9 years ago

0

@singleton I am trying to make a program that generates a html img tag for each file i have in my directory that is what i am trying to make and that you for awnsering

? [stefanking56]
9 years ago | edited 9 years ago

1

@lexostras

Here is how I would do it.

First, rename all the images in the directory to an increasing number like 1.jpg,2.jpg,3.jpg….1998.jpg,1999.jpg,2000.jpg
There are lots of batch and bash scripts to do this and you can also do it via Windows Explorer in Windows, just google it.

Then you can use php or any other program and just do a for loop

// With PHP  

for($i = 0; $i < 2001; $i++) {  
    echo '<img src="' . $i . '.jpg">';  
}  
p4p1 [lexostras]
9 years ago

0

@stefanking56 Omg dude thank you really thank you. You idear is great i am gonna do it tomorrow :) i love it seriously thx i would have never thought about it thank you.

Mr. Cyph3r [MrCyph3r]
9 years ago | edited 9 years ago

0

You could do that without renaming the files anyway, so you can preserve names if they make sense for the image… just to say.

EDIT: I suggest you to spend some more time on the basics of C… it can be damn useful.

singleton
9 years ago | edited 9 years ago

0

No need to rename the files. I’d go with something like this in the shell (bash):
for i in *; do echo "<img src=\"${i}\">"; done
and you can also redirect the output to a file with > 2000imgs.html

You must be logged in to reply to this discussion. Login
1 of 7

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss