473,378 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

To read the file after reading the filename from the directory

Hi All,
I'm trying to open a directory and after getting the file names from the the directory i need to read the contents of the file.

OS ----Unix
Language C

I have done the following:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    DIR *dirPtr;
  4.    struct dirent *filenamePtr;
  5.    FILE *fp = NULL;
  6.    long int FILESIZE = 0;
  7.    char *buffer = NULL;
  8.    char *temp = NULL;
  9.    int i = 0;
  10.    int length = 0;
  11.    int Rfseek = 0;
  12.    size_t bytesRead = 0;
  13.    char *name1 = ".";
  14.    char *name2 = "..";
  15.    char *space = " ";
  16.    int valName1 = 0;
  17.    int valName2 = 0;
  18.    const char *dirName = "/home/shilpabo/testresults";
  19.  
  20.    buffer = (char*)malloc(sizeof (char) *200);
  21.    memset(buffer, 0x00 , sizeof (char) *200) ;
  22.    temp = buffer;
  23.  
  24.    dirPtr = opendir(dirName);
  25.    if(dirPtr == NULL)
  26.    {
  27.      printf("opendir failed\n");
  28.      return 0;
  29.    }
  30.  
  31.    else if(dirPtr != NULL)
  32.    {
  33.      printf("Directory is now opened to read the files\n");
  34.  
  35.  
  36.      while((filenamePtr = readdir(dirPtr)) != NULL)
  37.      {
  38.        printf("The files are %s\n", filenamePtr->d_name);
  39.  
  40.        valName1 = strcmp(name1,  filenamePtr->d_name);
  41.        valName2 = strcmp(name2,  filenamePtr->d_name);
  42.  
  43.        if((valName1 != 0) && (valName2 != 0))
  44.        {
  45.  
  46.          strcpy(buffer,filenamePtr->d_name);
  47.          buffer += strlen(filenamePtr->d_name);
  48.          length = strlen(filenamePtr->d_name);
  49.  
  50.           strcpy(buffer, space);
  51.          buffer++;
  52.  
  53.  
  54.          fp = fopen(filenamePtr->d_name, "r");
  55.          if(fp == NULL)
  56.          {
  57.            printf("The file %s cannot be opened\n", filenamePtr->d_name);
  58.          }
  59.  
  60.          Rfseek = fseek(fp, 0, SEEK_END) ;
  61.  
  62.          FILESIZE = ftell( fp ) ;
  63.  
  64.          rewind(fp) ;
  65.  
  66.          bytesRead = fread(buffer, 1, FILESIZE, fp) ;
  67.          buffer += FILESIZE;
  68.  
  69.         fclose(fp);
  70.  
  71.        }
  72.  
  73.       }
  74.     }
  75.  
  76.      closedir(dirPtr);
  77.      printf("buffer = %s\n", temp);
  78.  
  79.  
  80.    return 0;
  81.  
  82. }
This program when i test on clear case in my Application gives an error:
The file XXXX cannot be opened.

I tried a lot but could not find out the mistake.

Kindly help me out..Its high priority..

Kind Reagrds,
Shilpa
Apr 3 '07 #1
0 1176

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Chris | last post by:
I need to monitor a bunch of files to see what is being opened, read, and closed, with no modifications being made. This is being done first on Windows machines, and later on Solaris. Looking...
2
by: GB | last post by:
Hi Everybody! I have 2 different processes/application. One is writing to a file and another is reading from it. For some reason the code doesnt seems to work and gives mscorlib.dll IOException...
0
by: Peter | last post by:
I am having a problem reading an Excel file that is XML based. The directory I am reading contains Excel files that can be of two types. Either generic Microsoft based or XML based. I am reading...
5
by: Mitchell S. Honnert | last post by:
Is there a way, given the full path of a folder on a network, that one can programatically tell if you have Read access to that folder? I have an application where the user is able to select a...
4
by: Matt Jensen | last post by:
Howdy I've got a rather strange issue occuring. I used forms based .NET authentication, although I'm also setting some session variables when people login. However, I've found when people use...
6
by: PipedreamerGrey | last post by:
I'm using the script below (originally from http://effbot.org, given to me here) to open all of the text files in a directory and its subdirectories and combine them into one Rich text file...
1
by: Peri | last post by:
Dear All, I am developing 2 applications. The first application will keep on generate a new file in a span of 30 milliseconds with some valid data inside (This code is written in C). The...
185
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will...
0
by: svpriyan | last post by:
Hai Implemented a code that will read the 100s of files from a directory. Each time when i read a file , i will take that file and check the content of the file( i put them in to a List )and ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.