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 8026009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:20:57+00:00 2026-06-04T23:20:57+00:00

I’m really new to C(I have been learning Cuda and wanted to learn C

  • 0

I’m really new to C(I have been learning Cuda and wanted to learn C so I can run everything together instead of generating data in Java/Python and copy/pasting it manually to my Cuda program to play with). I am trying to open a large file(20+gb) and parse some data but because I was having problems I decided to try to break down my problem first to verify I can open and read line by line a file and then in another file take a sample of the output of a line and try to parse it, then if all goes well I can simply bring them together. I managed(after a bit of a struggle) to get each part working but when I combine them together it doesn’t work(I tried in both eclipse & QT creator. QT creator says it exited unexpectedly and eclipse just stops..).

Here’s the parsing part(it works exactly as I want):

#include <string.h>
#include <stdio.h>

int main()
{
    char line[1024] = "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 4],";
    size_t len = strlen(line);
    memmove(line, line+1, len-3);
    line[len-3] = 0;
    printf("%s \n",line);


    char str[100], *s = str, *t = NULL;

    strcpy(str, line);
    while ((t = strtok(s, " ,")) != NULL) {
        s = NULL;
        printf(":%s:\n", t);
    }
    return 0;
}

The data in variable line is exactly as I get if I print each line of a file. But when I copy/paste it into my main program then it doesn’t work(QT shows nothing and eclipse just shows the first line). This code is just a few file IO commands that are wrapped around the commands above(everything within the while loop is the exact same as above)

#include <stdio.h>
#include <stdlib.h>

int main() {

    char line[1024];
    FILE *fp = fopen("/Users/me/Desktop/output.txt","r");

    printf("Starting.. \n");

    int count = 0;
    int list[30]; //items will be stored here

    while(fgets(line, 1024, fp) != NULL){
        count++;
        //char line[1024] = "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 4],";
        size_t len = strlen(line);
        memmove(line, line+1, len-4);
        line[len-4] = 0;
        printf("%s \n",line);

        char *s = str, *t = NULL;

        strcpy(str, line);
        while ((t = strtok(s, " ,")) != NULL) {
            s = NULL;
            printf(":%s:\n", t);
        }
    }
    printf(" num of lines is %i \n", count);
    return 0;
}

eclipse output:

Starting.. 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 4 

I can’t figure out what I’m doing wrong. I’m using a mac with gcc 4.2, if that helps. The only thing I can think of is maybe I’m doing something wrong the way I parse the list variable so its not accessible later on(I’m not sure its a wild guess). Is there anything I’m missing?

EDIT: if I comment out this block of code, it goes through the file(but doesn’t parse):

while ((t = strtok(s, " ,")) != NULL) {
    s = NULL;
    printf(":%s:\n", t);
}

the contents of the file is basically:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 4],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 4],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 2, 2, 4],

and so on(20+gb of that).

EDIT2: I just wanted to test if it was with my mac or not and I tried it on centos 5(installed it just to test) and I got the same error when running from command line except at the end it said Segmentation fault. All I did was copy the sample output above into a file, and changed the path in my code above and ran it.

  • 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-04T23:21:00+00:00Added an answer on June 4, 2026 at 11:21 pm

    I got it! Try inserting #include <string.h> with the other includes. I don’t know why you were using functions <string.h> it without including it but it caused incorrect linkage. The compiler issued the following warnings:

    test2.c:17: warning: incompatible implicit declaration of built-in function ‘strlen’
    test2.c:18: warning: incompatible implicit declaration of built-in function ‘memmove’
    test2.c:24: warning: incompatible implicit declaration of built-in function ‘strcpy’
    test2.c:25: warning: assignment makes pointer from integer without a cast
    

    The first three warnings indicate that you did not include the header file.
    The last one just goes to show that without declarations, functions are assumed to take and return integers (this is for backwards compatibility with ancient C code).

    Don’t forget that fgets includes the newline character at the end of the line. You need to account for that by changing len-3 to len-4.

    Also, don’t forget to fclose the file when you are done with it.

    Also, your memmove is unnecessary as you can just point to the second character:

    char *moved_line = line + 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.