Program Stopped Working?

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
Hi there,

when it reaches i = 0 after it checks that the filename starts with 201,it is able to move the file but it stops working after a while.

Code:
/*program to list all files in a directory*/

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

int main (void)
     {
       DIR *dp;
       struct dirent *ep;
       dp = opendir ("./");
       char name[80];
       if (dp != NULL)
         {
        while (ep = readdir (dp)){
		char *s = ep->d_name;
		puts(s);
		int i = strncmp(s,"201",3);//compare up to the first two character
		if(i == 0){
			char cat[] = "../";
			strcat(cat,s);
			printf("%s\n",cat);
			printf("%s\n",s);
			rename(s,cat);		
		}
		
	    }
	    closedir (dp);
         }
       else perror ("Couldn't open the directory");
       return 0;
}
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Hi there,

when it reaches i = 0 after it checks that the filename starts with 201,it is able to move the file but it stops working after a while.

Code:
/*program to list all files in a directory*/

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

int main (void)
     {
       DIR *dp;
       struct dirent *ep;
       dp = opendir ("./");
       char name[80];
       if (dp != NULL)
         {
        while (ep = readdir (dp)){
		char *s = ep->d_name;
		puts(s);
		int i = strncmp(s,"201",3);//compare up to the first two character
		if(i == 0){
			char cat[] = "../";
			strcat(cat,s);
			printf("%s\n",cat);
			printf("%s\n",s);
			rename(s,cat);		
		}
		
	    }
	    closedir (dp);
         }
       else perror ("Couldn't open the directory");
       return 0;
}

There are a few flaw in the program above, however, you have to be specific in mentioning what it stops working after a while, what do you mean by it stops after awhile ? Are you expecting the loop to run forever ?

Read up on "strcat" C function. Are you sure that is the right way to use it ? How much memory is allocated for your first argument ? Can you actually be "safe" concatenating more characters into it. Please don't mix up between Java array and C array usage.

Also what are you trying to achieve here ? As a word of advice, I would highly recommend that when you try to articulate your problem, be specific, be Informative. When describing, try to think in the shoe of the audience, do they know everything that you know, including the context, the intention, the expected output, and what is being observed. It is unlikely we will be running your code to find out what you experienced. It's better that you be as specific and informative as possible, so that you are helping others to understand your problem and you get your problem solved correctly and sooner.
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
Hi there,

when it reaches i = 0 after it checks that the filename starts with 201,it is able to move the file but it stops working after a while.

Code:
/*program to list all files in a directory*/

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

int main (void)
     {
       DIR *dp;
       struct dirent *ep;
       dp = opendir ("./");
       char name[80];
       if (dp != NULL)
         {
        while (ep = readdir (dp)){
		char *s = ep->d_name;
		puts(s);
		int i = strncmp(s,"201",3);//compare up to the first two character
		if(i == 0){
			char cat[] = "../";
			strcat(cat,s);
			printf("%s\n",cat);
			printf("%s\n",s);
			rename(s,cat);		
		}
		abort()//added the line here
	    }
	    closedir (dp);
         }
       else perror ("Couldn't open the directory");
      
       return 0;
}


hi,on strcat(dest,src),i copy the ../ from the filename so that the files that starts with 201 can be moved from one direction to another.when the say the program stop working I mean that there is a pop-up windows that say the program stop working and there is a some sort grey out.
for batch file i read that move instruction and need to write some regex,can c do regex btw?

i propose using c to move the file upwards because java doesn't have OS instructions,python is hard to deploy on multiple computers,whereas for C just need to run exe,btw this is to be run every morning so that the relevant files can be shifted up one directory.

right now, i need to write a program..maybe usin strtok() ... not sure if can do string match 20130408 on this kind of date format..
need to create folders in c,seperate out by june,july...oct etc.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
hi,on strcat(dest,src),i copy the ../ from the filename so that the files that starts with 201 can be moved from one direction to another.when the say the program stop working I mean that there is a pop-up windows that say the program stop working and there is a some sort grey out.
for batch file i read that move instruction and need to write some regex,can c do regex btw?

i propose using c to move the file upwards because java doesn't have OS instructions,python is hard to deploy on multiple computers,whereas for C just need to run exe,btw this is to be run every morning so that the relevant files can be shifted up one directory.

right now, i need to write a program..maybe usin strtok() ... not sure if can do string match 20130408 on this kind of date format..
need to create folders in c,seperate out by june,july...oct etc.

Are you actually reading my post to you ? I asked you to go and read up strcat manual, see what is wrong with your code. C don't have Regex on it's own. GNU C Library do have a regex library. Read more here at Comparison of regular expression engines - Wikipedia, the free encyclopedia

Java doesn't require OS file operations exposed at the API layer to perform these stuffs that you mentioned. Java API already abstract file operations via it's File class. Please go read up on it.

As for the portability of Python, I think you are greatly mistaken. Python is well support in Windows and various unix platforms. Same goes for Perl, Ruby, PHP. I don't see how a C program is more portable unless you have to resort to compilation across various platforms.

How is installing the necessary interpreter or VM for these scripting languages a difficult task in deployment ? If you are proficient in development and deployment work, you will find interpreted languages are more productive than native codes very often, especially when coming to short tasks.

Perhaps you want to be more specific on which platform are you working on ?
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
maybe char c [4] declare a size .. will be better,

how can i write a file to run for python,something like batch file will automatically call the instructions,

for GNU C,how can i install it into borland,

can C command create directory?

Are you actually reading my post to you ? I asked you to go and read up strcat manual, see what is wrong with your code. C don't have Regex on it's own. GNU C Library do have a regex library. Read more here at Comparison of regular expression engines - Wikipedia, the free encyclopedia

Java doesn't require OS file operations exposed at the API layer to perform these stuffs that you mentioned. Java API already abstract file operations via it's File class. Please go read up on it.

As for the portability of Python, I think you are greatly mistaken. Python is well support in Windows and various unix platforms. Same goes for Perl, Ruby, PHP. I don't see how a C program is more portable unless you have to resort to compilation across various platforms.

How is installing the necessary interpreter or VM for these scripting languages a difficult task in deployment ? If you are proficient in development and deployment work, you will find interpreted languages are more productive than native codes very often, especially when coming to short tasks.

Perhaps you want to be more specific on which platform are you working on ?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
maybe char c [4] declare a size .. will be better,

how can i write a file to run for python,something like batch file will automatically call the instructions,

for GNU C,how can i install it into borland,

can C command create directory?

Well how method you use to get memory is not important, important part is why do you think concatenate a string into another doesn't require more memory.

When doing C programming, memory is not automatically managed. You have to instruct the OS to allocate memory to your program. Also you have to return memory back to the OS. All these must be done explicitly. Please read this c++ - What is the difference between char a[] = "string"; and char *p = "string"; - Stack Overflow

Borland C/C++ is Windows platform compiler. GNU C Library is for unix environment, unless you are running Cygwin in Windows.

For creating directory, I don't think it is part of C standard. It's a platform specific operation to C libraries. in unices you have the POSIX "mkdir" function. For windows, I could be mistaken, but I think you will need to use WIN32 CreateDirectory API.

I failed to understand if it is not necessary to do it in C, why make it so hard for yourself ? You wouldn't get a prize doing in C, do you ? Not unless you are trying to learn, then please go ahead.
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top