Domanda

So I'm reading in files and outputting the contents of the files onto the console using system calls. Also I want to add a space to the output, every 20 lines I encounter. This is where I'm having trouble, in that despite the following few lines of code, the entire file is being displayed without the spaces

     //Write file contenst
  while( (nReadFile = read(nOpenFile, buffer, 1) != 0))
{
  write(1, &nLineCount, sizeof(nLineCount));

  if(nLineCount == 20)
    {
      write(1, "\n", 2);
      nLineCount = 0;
    }

  if(nReadFile = write(1, buffer, nReadFile) == '\n')
    {
      nLineCount++;
    }

}

Here is the entire program (Excluding the .h file containing the libraries)

  #include"mymore.h"

int main(int argCount, char *argv[])
{

  struct termios initial_settings, new_settings;

  tcgetattr(fileno(stdin), &initial_settings);
  new_settings=initial_settings;
  new_settings.c_lflag &= ~ICANON;
  new_settings.c_lflag &= ~ECHO;
  new_settings.c_cc[VMIN] = 1;
  new_settings.c_cc[VTIME] = 0;
  if (tcsetattr(fileno(stdin), TCSANOW, &new_settings)!=0)
    {
      fprintf(stderr, "could not set attributes\n");
    }

  int nLineCount = 0;

  int nCheckFile = 0;
  int nFileCountCounter = 1; //first arguement interested in is argv[1]
  int nOpenFile = 0;
  int nReadFile = 0;
  int nCount = 0;
  char *cData;
  char buffer[0];

  //check that arguements have been provided
  if( argCount < 2)
    {
      write(1, "There needs to be at least one file provided! \n", 50);
      return 1;
    }

  do 
    {
  printf("%d", argCount);
  //check if file exists
  nCheckFile = access(argv[nFileCountCounter], F_OK);

  if(nCheckFile != 0) //if file does not exist
{
  write(1, "The file ", 10);
  write(1, argv[nFileCountCounter], strlen(argv[1]));
  write(1," does not exist! \n", 25);
  return 1;
}
  else //file does exist
{
  write(1, "Opening ", 10);
  write(1, argv[nFileCountCounter], strlen(argv[1]));
  write(1, "\n", 2);
}

  //open the file
  nOpenFile = open(argv[nFileCountCounter], O_RDONLY);

  if(nOpenFile < 0)
{
  write(1, "Failed to open file ", 10);
  write(1, argv[nFileCountCounter], strlen(argv[nFileCountCounter]));
  write(1, "\n", 2);
  return 1;
}

  //read file
  cData = (char *) malloc(100 * sizeof(char));



  cData[nReadFile] = '\0';//append null terminator

  //find length of source file
  while(cData[nCount] != 0)
{
  nCount++;
}

  //Write file contenst
  while( (nReadFile = read(nOpenFile, buffer, 1) != 0))
{
  write(1, &nLineCount, sizeof(nLineCount));

  if(nLineCount == 20)
    {
      write(1, "\n", 2);
      nLineCount = 0;
    }

  if(nReadFile = write(1, buffer, nReadFile) == '\n')
    {
      nLineCount++;
    }

}

  cout << nLineCount << endl;
  //close file
  close(nOpenFile);

  //Increment to next file
  nFileCountCounter++;

}while(nFileCountCounter < argCount);//while there are still arguements

  tcsetattr(fileno(stdin), TCSANOW, &initial_settings); 
  return 0;
}

This is actually my first experience using system calls, and one thing I think I've noticed is that the write command is being executed before any of the c code?

Any ideas? Thanks

È stato utile?

Soluzione

There are plenty of mistakes in your code:

  1. char buffer[0]; should be char buffer[1];

    char buffer[0]; defines a buffer which can store zero character, I suppose it is not what you want.

  2. write(1, "There needs to be at least one file provided! \n", 50); should be

    char *msg = "There needs to be at least one file provided! \n";
    write(1, msg, strlen(msg));
    

    the message is longer than 50. There are serval similar mistakes, and can be fixed similarly.

  3. while( (nReadFile = read(nOpenFile, buffer, 1) != 0)) should be

    while( (nReadFile = read(nOpenFile, buffer, 1)) != 0)
    

    In C, operator != has higher precedence than =, so nReadFile = read(nOpenFile, buffer, 1) != 0 meas nReadFile = (read(nOpenFile, buffer, 1) != 0).

  4. if(nReadFile = write(1, buffer, nReadFile) == '\n')

    If you want to check whether the currect character is a newline, you should to something like if (buffer[0] == '\n').

  5. cout << nLineCount << endl;

    This is C++, not C.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top