Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8805539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:53:37+00:00 2026-06-14T01:53:37+00:00

I wrote the following code: It should accept a file name and create it

  • 0

I wrote the following code:

It should accept a file name and create it and write to it. Nothing happens.
I don’t understand why. I tried searching and saw similar examples should work fine.
I am using VirtualBox with Xubuntu if that matters.

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <assert.h>
#include <errno.h> 
#include <string.h>

#define SIZE_4KB        4096
#define FILE_SIZE       16777216



/*Generate a random string*/
char* randomStr(int length)
{
        int i = -1;
        char *result;
        result = (char *)malloc(length);
        while(i++ < length)
        {
                *(result+i) = (random() % 23) + 67;
                if(i%SIZE_4KB)
                        *(result+i) = '\0';
        }
        return result;
}

void writeFile(int fd, char* data, int len, int rate)
{
        int i = 0;
        len--;
        printf("Writing...\n");
        printf("to file %d :", fd);
        while(i < len)
        {
                write(fd, data, rate);
                i += rate;
        }
}

int main(int argc, char** argv)
{
        int i = -1, fd;
        char *rndStr;
        char *filePath;      
        assert (argc == 2);
        filePath = argv[1];
        rndStr = randomStr(FILE_SIZE);
        printf("The file %s was not found\n", filePath);
        fd = open(filePath, O_CREAT, O_WRONLY);
        writeFile(fd, rndStr, FILE_SIZE, SIZE_4KB);
        return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:53:38+00:00Added an answer on June 14, 2026 at 1:53 am

    The open call is wrong. Multiple flags are specified by combining arguments with OR. And, when you are creating a file, the third argument should be the permissions you wish the file to have. So your open call should be something like:

    fd = open(filePath, O_CREAT | O_WRONLY, 0666);
    if (fd < 0)
        Handle error…
    

    You should always test return values from system calls and library functions to see if errors have occurred.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i wrote following code to create a linkbutton programmatically, but its showing like lable
I wrote the following code for text file encryption and decryption. The encryption process
if wrote the following code and do not understand why the trace returns false:
I wrote the following code to count the node of an XML file: private
I wrote the following code: [TestMethod] //[ExpectedException(typeof(UriFormatException), url should be well formatted.)] public void
I wrote the following piece of code on codepad but I do not understand
I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
I wrote the following code to select text from database,but when i echo the
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {
I wrote the following code snippet in VS2010: #pragma once #include stdafx.h #ifndef SECURITY_WIN32

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.