Thursday, February 28, 2013

Traverse all the files under one folders using matlab and C++ on windows

I usually need to process batches of files in my work. sometimes use matlab, some times use C++:

1. For MATLAB, it is very easy:

clear;
clc;
close all;
MyDir='D:\Matlab_Code\RA_6\recent-valid\'; 
% the folder where the targeted files are stored, do not forget the '\' in the end;
AllFileName=dir([MyDir,'*.txt']);
% AllFileName it is a array whose each element is a structure with many attributes
% all the infomation about txt files would be included in AllFilename
% You can even get the numbers of files just by size the AllFileName without traversing all the files
for i=1:length(AllFileName)
     Fid=fopen([MyDir,AllFileName(i).name]); % there are many optional parameters  you can choose
     Content=fgetl(Fid);
     while ischar(Content)
          Str= Content;
          % add your code here
          Content=fgetl(Fid);% if you miss this, it could run into a dead loop
     end
     fclose(Fid);% never forget to close the file
 end



2. For C\C++ on windows, it is almost same:


#include 
#include 
#include 
using namespace std;
int main (void)
{

    _finddata_t fileDir; // it is included in io.h, much similar to the AllFileName in the matlab code

    char* dir="C:\\CZG\\Matlab_Code\\HSS\\*.m";

    // notice the end of the above line: 
    // if you write as  *.m, then only m file would be found
    // if you write as  *.m*, hen all the extention format start with m could be found, such as .mat, .m file could be found at the same time

     long lfDir;

    if((lfDir = _findfirst(dir,&fileDir))==-1l) // in case there is no m file found

       printf("No file is found\n"); 

    else{

           printf("file list:\n");

           do{

               printf("%s\n",fileDir.name);

             }

           while( _findnext( lfDir, &fileDir ) == 0 );// the common way to tranverse the files, and you can not get the number of files without tranversing files, which is different from matlab

        }

    _findclose(lfDir);  // still do not forget to close

    unsigned int i=fileDir.size;

    printf("%d\n",i);

    return 0;

}

 another method is that you can put all the file names to one txt files using system function, then you can access all the files one by one through reading this txt file.

#include <iostream>
#include  <string>
#include <fstream>
using namesapce std;
int main()
{
system("dir /a-d /b D:\\zhuanfa_singapore\\step_1\\*.* >D:\\zhuanfa_singapore\\step_2\\list.txt");
// all the file names under D:\\zhuanfa_singapore\\step_1 will be stored in
//D:\\zhuanfa_singapore\\step_2\\list.txt
my_in.open("D:\\zhuanfa_singapore\\step_2\\list.txt",ios::in);
string str2="D:\\zhuanfa_singapore\\step_1\\";
 while(!my_in.eof())
 {
     string str1;
     getline(my_in,str1);
     string str3=str2+str8;
     fstream _file;
     _file.open(str3.c_str(),ios::in);
       if(!_file)
      {
         cout<<str3<<"not exist!"<<endl;
      }
      else
      {
         cout<<str3<<"already exsit!"<<endl;

      }
 }
 my_in.close();
return 0;
}


C\C++ on linux seems a little different, i will record that in another post.

Using php to send mail on apache (ubuntu)

I have an Ubuntu in Oracle VM VirtualBox, however i did not set it as a server, I just want to implement  sending  email on ubuntu.
Make it short:using sendmail.
installing sendmail on ubuntu


in the file:  /etc/php5/apache2/php.ini
make it like this:  sendmail_path = /usr/sbin/sendmail -t -i
sudo apt-get install sendmail
sudo apt-get install sendmail-cf

then we can use the following code to send mail:






this is only a test, you can change the code according to your demand.
done!

using php to send mail on iis(windows server 2008)

I know php might show better performance on apache, however it happened that I only have a windows server 2008 on amazon EC2 with iis installed. A senior student want me to finish the sending mail function with using php, so I have no other choice so far.

It seems very easy, every one know that php has the function mail(). But, this function only could easily run on linux almost without any configuration, while on windows, it is another story.

I spent days to find that there two basic methods:
1. directly use the SMPT service on iis
2. use some  other classes: phpmailer or swiftermailer

I choose the 2nd method first, unluckily, no matter how I configure, it  dose not work, so far, I still do not know why.(still think about it)

then I switched to 1st method,
in windows server2008, the smpt server is not installed by default, so you should install and configure them,
I use the following two steps to finish the task:
1:to install the smtp server:
http://www.iis.net/learn/application-frameworks/install-and-configure-php-on-iis/configure-smtp-e-mail-in-iis-7-and-above

2. configuration and code testing
http://geekswithblogs.net/tkokke/archive/2009/05/31/sending-email-from-php-on-windows-using-iis.aspx

done!

about the php session for ajax

In fact, there is nothing serious, i just want to say that the php session  in index.html and in the php files which is used to respond to the ajax could share the same $_SESSION[];

however, you should not forget one thing, in the php files responding to the ajax, should write:

session_start();


I only know this today, which grealy faciliate my work!