LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2013, 08:15 PM   #1
derstephen
Member
 
Registered: Sep 2011
Location: Northern Colorado
Distribution: Slackware64
Posts: 79

Rep: Reputation: Disabled
Weird Problem Writing '#'s to Mapped File in C


Hello,

I've pretty much finished this programming assignment, but I've hit a very strange, but small, snag. It's a pretty simple program: it takes a single word as an argument, prints the output of the 'banner' command with that word, then prints the negative of banner's output. Here's an example:
Code:
stephen> ./negbanner hello
Original banner output:

#     #  #######  #        #        #######  
#     #  #        #        #        #     #  
#     #  #        #        #        #     #  
#######  #####    #        #        #     #  
#     #  #        #        #        #     #  
#     #  #        #        #        #     #  
#     #  #######  #######  #######  #######  

Negative of banner output:

 %%%%% %%       %% %%%%%%%% %%%%%%%%       %%
 %%%%% %% %%%%%%%% %%%%%%%% %%%%%%%% %%%%% %%
 %%%%% %% %%%%%%%% %%%%%%%% %%%%%%%% %%%%% %%
       %%     %%%% %%%%%%%% %%%%%%%% %%%%% %%
 %%%%% %% %%%%%%%% %%%%%%%% %%%%%%%% %%%%% %%
 %%%%% %% %%%%%%%% %%%%%%%% %%%%%%%% %%%%% %%
 %%%%% %%       %%       %%       %%       %%
Of course you'll notice that the negative is made up of '%'s instead of '#'; therein lies my problem. The code that writes the '%'s looks like this:

Code:
if (bann_file[curr] == ' ')
    bann_file[curr] = '%';
Here, bann_file is a pointer to the temporary file, which is a shared file mapping via mmap() (the overwriting is done by child processes; one per line). So it's basically just a char*. And this obviously works fine. But when I change '%' to '#' I get this:

Code:
stephen> ./negbanner hello
Original banner output:

#     #  #######  #        #        #######  
#     #  #        #        #        #     #  
#     #  #        #        #        #     #  
#######  #####    #        #        #     #  
#     #  #        #        #        #     #  
#     #  #        #        #        #     #  
#     #  #######  #######  #######  #######  

Negative of banner output:









stephen>
I've tried redirecting the output to separate text files, and after running wc on them, it looks like the outputs contain the exact same number of characters, so the characters are all there, but something's getting lost in translation. Obviously '#' is a special character in C, but it's not escapable; I've also tried using the ASCII code directly (35), but I get the same result. Anyone happen to know what may be going on?

I can provide more info on the implementation if need be, but seeing as how it works with other characters I'd be surprised if that were needed. Thanks.

Last edited by derstephen; 03-05-2013 at 08:17 PM.
 
Old 03-05-2013, 09:21 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,046

Rep: Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798
Somewhere in your code you must be translating '#' to space characters, else there would be no space characters in your first example. Now, consider what happens if this occurs after you've replaced all the original space characters to '#', thus leaving lines containing nothing but "##########################################".
 
1 members found this post helpful.
Old 03-05-2013, 10:11 PM   #3
derstephen
Member
 
Registered: Sep 2011
Location: Northern Colorado
Distribution: Slackware64
Posts: 79

Original Poster
Rep: Reputation: Disabled
Oh god...

I had two successive 'if' statements instead of an 'if' followed by an 'else if'. How embarrassing!

Thank you rknichols.
 
Old 03-07-2013, 02:43 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,433

Rep: Reputation: 203Reputation: 203Reputation: 203
Hmmm!
I think it would look better if you did not negate the space between letters.
Could you just do that one little thing?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird File Problem bighippo999 Linux - Newbie 2 10-05-2010 12:21 AM
Network device Memory mapped or Port mapped ?? Bignon Linux - Hardware 0 10-20-2009 09:36 AM
Looking for Equivalent to File Manager with FTP mapped Suncoast Linux - Desktop 13 05-05-2009 02:16 PM
Weird File Problem sashhoney Linux - Software 1 05-26-2005 05:23 PM
Memory Mapped file IO problems legogt Programming 0 08-01-2003 02:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration