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

  • Home
  • SEARCH
  • 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 8896035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:56:24+00:00 2026-06-14T23:56:24+00:00

I try to create HTML file programaticatically. But generated file shown me as blank

  • 0

I try to create HTML file programaticatically. But generated file shown me as blank file. I don’t know the issue.

Here is my code:

- (IBAction) createFileAction
{
    NSLog(@"************* createFileAction *************");
    NSMutableString *htmlString = [[NSMutableString alloc] init];
    NSLog(@"%@",htmlString);
    [htmlString appendString:@"<html><head><title></title><style type='text/css'>.style1{height: 27px;}#Text1{height: 19px;width: 210px;}.style2{width: 255px;}.style3{height: 27px;width: 255px;}#petValue{width: 206px;}#nameValue{width: 206px;}#dateValue{width: 209px;}"];
    [htmlString appendString:@"#Text2{width: 215px;}#Text3{width: 210px;height: 22px;}#Text4{width: 209px;}.style8{width: 13px;}.style9{ width: 263px;}.style10{height: 27px;width: 263px;}"];
    [htmlString appendString:@".style11{width: 418px;}.style12{width: 199px;}.style13{height: 27px; width: 199px;}.style14{height: 24px;}.style15{width: 410px;}</style> </head>"];
    [htmlString appendString:@"<body>"];

    NSString *originalString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.makepartsfast.com/2012/09/4337/more-3d-printing-in-metals-ex-one-introduces-the-m-flex-3d-printing-system/"] encoding:NSUTF8StringEncoding error:nil];
    NSScanner *scanner = [NSScanner scannerWithString:originalString];
    NSString *extractedString = nil;


    [scanner scanUpToString:@"<div class=\"postarea\">" intoString:nil];
    [scanner scanString:@"<div class=\"postarea\">" intoString:nil];



    [scanner scanUpToString:@"<div style=\"clear:both;\">" intoString:&extractedString];


    if (extractedString) 
    {
        // string was extracted
        NSLog(@"%@", extractedString);
    }   
    NSLog(@"BEFORE= %@",htmlString);
    [htmlString appendString:extractedString];
     NSLog(@"AFTER= %@",htmlString);
    [htmlString appendString:@"</body></html>"];
    NSLog(@"FINAL=%@",htmlString);

    // check to see if file already exists in documents directory
    if([self.fileMgr fileExistsAtPath:self.file])
    {
        NSLog(@"File already exists");
    }
    else 
    {
        NSLog(@"file does not exist");
        NSLog(@"Try creating the file");

        // file does not already exist so create it
        [self.fileMgr createFileAtPath:file contents:nil attributes:nil];

        NSLog(@"%@",htmlString);
        [htmlString writeToFile:self.file atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];


        //Test again whether this file exists now that we have tried creating it
        if([self.fileMgr fileExistsAtPath:self.file])
        {
            NSLog(@"File exists now");
        }
        else 
        {
            NSLog(@"File still doesn't exist");
        }

    }
}

In this method, I get only select portion from the URL in extractedString in html format & append it to htmlstring. when i try to append different string then it works fine.

  • 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-14T23:56:26+00:00Added an answer on June 14, 2026 at 11:56 pm

    It’s because you passed NSStringEncodingConversionAllowLossy as the encoding argument. You should be passing one of the string encoding arguments. What’s happening is that NSStringEncodingConversionAllowLossy is equal to 1. But writeToFile:atomically:encoding:error: is expecting a string encoding constant. The string encoding constant equal to 1 is NSASCIIStringEncoding. But your string can’t be converted to ASCII, so you get an empty file. Change that line to use UTF8 and you get the expected non-empty file:

    [htmlString writeToFile:self.file atomically:NO encoding:NSUTF8StringEncoding error:nil];
    

    You had trouble with this at least partly because you are not checking return types or error conditions. The method call returns BOOL and will create an NSError if anything goes wrong. This would have helped you figure out what was wrong, if you were listening. If you had done this:

    NSError *saveError = nil;
    if (![htmlString writeToFile:file atomically:NO encoding:NSStringEncodingConversionAllowLossy error:&saveError]) {
        NSLog(@"Error writing file: %@", saveError);
    }
    

    You would have seen an error message reading something like:

    2012-11-21 11:20:47.250 Untitled[5731:707] Error writing file: Error Domain=NSCocoaErrorDomain Code=517 "The file “junk.html” couldn’t be saved using text encoding Western (ASCII)." UserInfo=0x7ffc0860b7c0 {NSFilePath=/tmp/junk.html, NSStringEncoding=1
    

    That might not be obvious at first but the fact that it specifically mentions trying to use ASCII would have been a huge clue to the source of the problem.

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

Sidebar

Related Questions

i try to create image resizer tool using jquery UI. here is the file
When I try to read an HTML file (using HTMLWorker.parseToList) and create a PDF
I try to create an html helper with a control from Telerik but to
I try create a map for open ~/.vimrc, but open the ~/.vimrc only when
I try to create styles with first-child and last-child items but I encountered a
I try to create a new file inside a JSP and try to save
I've generated a HTML file and the top html declaration looks like this: <html
I'm creating a docx file from an HTML page using pandoc , but for
When trying to convert excel file to Html using org.apache.poi.ss.examples.html.ToHtml.create(...), the call crashes with
I have install ZendFramewrok with instruction on: http://framework.zend.com/manual/en/learning.quickstart.create-project.html and when I try to run

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.