Thread: Having some issues with text formatting

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    5

    Having some issues with text formatting

    I'm currently working through the book Head First C and have gotten to a point where I am taking gps coordinates and converting them to JSON format and then putting them into a file. The issue I have run into is that running the program and directing the output to a file, or to the console, from a file cause strange formatting issues.

    Here is the code so far:
    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
        float latitude;
        float longitude;
        char info[80];
        int started = 0;
        puts("data=[");
        while (scanf("%f, %f, %79[^\n]", &latitude, &longitude, info) == 3)
        {
            if (started)
                printf(", \n");
            else
                started = 1;
            
            printf("{latitude: %f, longitude: %f, info: '%s'}", latitude, longitude, info);
        }
        puts("\n]");
        return 0;
    }
    This is the .csv file
    Code:
    42.363400,-71.098465,Speed = 21
    42.363327,-71.097588,Speed = 23
    42.363255,-71.096710,Speed = 17
    42.363182,-71.095833,Speed = 22
    42.363110,-71.094955,Speed = 14
    42.363037,-71.094078,Speed = 16
    42.362965,-71.093201,Speed = 18
    42.362892,-71.092323,Speed = 22
    42.362820,-71.091446,Speed = 17
    42.362747,-71.090569,Speed = 23
    42.362675,-71.089691,Speed = 14
    42.362602,-71.088814,Speed = 19
    42.362530,-71.087936,Speed = 16
    42.362457,-71.087059,Speed = 16
    42.362385,-71.086182,Speed = 21
    The resulting .json file:
    Code:
    data=[
    {latitude: 42.363400, longitude: -71.098465, info: 'Speed = 21
    '}, 
    {latitude: 42.363327, longitude: -71.097588, info: 'Speed = 23
    '}, 
    {latitude: 42.363255, longitude: -71.096710, info: 'Speed = 17
    '}, 
    {latitude: 42.363182, longitude: -71.095833, info: 'Speed = 22
    '}, 
    {latitude: 42.363110, longitude: -71.094955, info: 'Speed = 14
    '}, 
    {latitude: 42.363037, longitude: -71.094078, info: 'Speed = 16
    '}, 
    {latitude: 42.362965, longitude: -71.093201, info: 'Speed = 18
    '}, 
    {latitude: 42.362892, longitude: -71.092323, info: 'Speed = 22
    '}, 
    {latitude: 42.362820, longitude: -71.091446, info: 'Speed = 17
    '}, 
    {latitude: 42.362747, longitude: -71.090569, info: 'Speed = 23
    '}, 
    {latitude: 42.362675, longitude: -71.089691, info: 'Speed = 14
    '}, 
    {latitude: 42.362602, longitude: -71.088814, info: 'Speed = 19
    '}, 
    {latitude: 42.362530, longitude: -71.087936, info: 'Speed = 16
    '}, 
    {latitude: 42.362457, longitude: -71.087059, info: 'Speed = 16
    '}, 
    {latitude: 42.362385, longitude: -71.086182, info: 'Speed = 21
    '}
    ]
    If the program is redirected to the console:
    Code:
    data=[
    '}, itude: 42.363400, longitude: -71.098465, info: 'Speed = 21
    '}, itude: 42.363327, longitude: -71.097588, info: 'Speed = 23
    '}, itude: 42.363255, longitude: -71.096710, info: 'Speed = 17
    '}, itude: 42.363182, longitude: -71.095833, info: 'Speed = 22
    '}, itude: 42.363110, longitude: -71.094955, info: 'Speed = 14
    '}, itude: 42.363037, longitude: -71.094078, info: 'Speed = 16
    '}, itude: 42.362965, longitude: -71.093201, info: 'Speed = 18
    '}, itude: 42.362892, longitude: -71.092323, info: 'Speed = 22
    '}, itude: 42.362820, longitude: -71.091446, info: 'Speed = 17
    '}, itude: 42.362747, longitude: -71.090569, info: 'Speed = 23
    '}, itude: 42.362675, longitude: -71.089691, info: 'Speed = 14
    '}, itude: 42.362602, longitude: -71.088814, info: 'Speed = 19
    '}, itude: 42.362530, longitude: -71.087936, info: 'Speed = 16
    '}, itude: 42.362457, longitude: -71.087059, info: 'Speed = 16
    '}atitude: 42.362385, longitude: -71.086182, info: 'Speed = 21
    ]

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,743
    I cannot replicate your output. Either way, I get:
    Code:
    data=[
    {latitude: 42.363400, longitude: -71.098465, info: 'Speed = 21'}, 
    {latitude: 42.363327, longitude: -71.097588, info: 'Speed = 23'}, 
    {latitude: 42.363255, longitude: -71.096710, info: 'Speed = 17'}, 
    {latitude: 42.363182, longitude: -71.095833, info: 'Speed = 22'}, 
    {latitude: 42.363110, longitude: -71.094955, info: 'Speed = 14'}, 
    {latitude: 42.363037, longitude: -71.094078, info: 'Speed = 16'}, 
    {latitude: 42.362965, longitude: -71.093201, info: 'Speed = 18'}, 
    {latitude: 42.362892, longitude: -71.092323, info: 'Speed = 22'}, 
    {latitude: 42.362820, longitude: -71.091446, info: 'Speed = 17'}, 
    {latitude: 42.362747, longitude: -71.090569, info: 'Speed = 23'}, 
    {latitude: 42.362675, longitude: -71.089691, info: 'Speed = 14'}, 
    {latitude: 42.362602, longitude: -71.088814, info: 'Speed = 19'}, 
    {latitude: 42.362530, longitude: -71.087936, info: 'Speed = 16'}, 
    {latitude: 42.362457, longitude: -71.087059, info: 'Speed = 16'}, 
    {latitude: 42.362385, longitude: -71.086182, info: 'Speed = 21'}
    ]
    which differs from both your sample outputs, and is what I expected.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    5
    That's really weird. What OS are you using? I'm on Ubuntu 18.04.2

  4. #4
    Registered User
    Join Date
    Mar 2019
    Posts
    5
    Quote Originally Posted by laserlight View Post
    I cannot replicate your output. Either way, I get:
    Code:
    data=[
    {latitude: 42.363400, longitude: -71.098465, info: 'Speed = 21'}, 
    {latitude: 42.363327, longitude: -71.097588, info: 'Speed = 23'}, 
    {latitude: 42.363255, longitude: -71.096710, info: 'Speed = 17'}, 
    {latitude: 42.363182, longitude: -71.095833, info: 'Speed = 22'}, 
    {latitude: 42.363110, longitude: -71.094955, info: 'Speed = 14'}, 
    {latitude: 42.363037, longitude: -71.094078, info: 'Speed = 16'}, 
    {latitude: 42.362965, longitude: -71.093201, info: 'Speed = 18'}, 
    {latitude: 42.362892, longitude: -71.092323, info: 'Speed = 22'}, 
    {latitude: 42.362820, longitude: -71.091446, info: 'Speed = 17'}, 
    {latitude: 42.362747, longitude: -71.090569, info: 'Speed = 23'}, 
    {latitude: 42.362675, longitude: -71.089691, info: 'Speed = 14'}, 
    {latitude: 42.362602, longitude: -71.088814, info: 'Speed = 19'}, 
    {latitude: 42.362530, longitude: -71.087936, info: 'Speed = 16'}, 
    {latitude: 42.362457, longitude: -71.087059, info: 'Speed = 16'}, 
    {latitude: 42.362385, longitude: -71.086182, info: 'Speed = 21'}
    ]
    which differs from both your sample outputs, and is what I expected.
    I ended up getting this output after making the csv with a different text editor. I guess notepadd++ must be doing something weird with the text on Ubuntu. Thanks for making sure I wasn't accidentally doing something weird code-wise

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    931
    Looks like it has Windows line endings, which are "\r\n".
    Try
    Code:
        while (scanf("%f, %f, %79[^\r\n]", &latitude, &longitude, info) == 3)
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,983
    Did you copy your .csv file from windows?

    Because it looks like the sort of thing that happens when you're not handling the '\r' character that Windows also uses for a newline.

    Unix/Linux = \n
    Dos/Windows = \r\n
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2019
    Posts
    5
    Quote Originally Posted by john.c View Post
    Looks like it has Windows line endings, which are "\r\n".
    Try
    Code:
        while (scanf("%f, %f, %79[^\r\n]", &latitude, &longitude, info) == 3)
    That works! Thank you. I think I'm gonna have to use a different text editor though lol

  8. #8
    Registered User
    Join Date
    Mar 2019
    Posts
    5
    No, I copied it from firefox on Ubuntu. I guess that could still have an effect on it. I'm testing some stuff with notepad++ right now to see if copying from firefox could still have that effect and so far making the .csv file in notepad++ is producing the desired outcome so it must be from the copy.

    <EDIT> I forgot to remove the \r from the scanf line it looks like notepad++ is using windows newline
    Last edited by Xenos; 03-09-2019 at 10:21 PM.

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    716
    Quote Originally Posted by Xenos View Post
    <EDIT> I forgot to remove the \r from the scanf line it looks like notepad++ is using windows newline
    You can change the line ending of a file in Notepad++ by using the Edit/EOL Conversion menu (and then save the file after changing it).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting Issues
    By Khabz in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2013, 02:06 PM
  2. Text formatting
    By newn in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2012, 05:33 AM
  3. Help with formatting text
    By C89 in forum C Programming
    Replies: 10
    Last Post: 03-15-2012, 11:54 PM
  4. Help formatting text in c
    By anonymoususer in forum C Programming
    Replies: 3
    Last Post: 11-06-2011, 02:30 PM
  5. need help with formatting text
    By Flikm in forum C++ Programming
    Replies: 1
    Last Post: 09-06-2001, 07:52 PM

Tags for this Thread