COKEDUDE 27 Junior Poster in Training

I know this is how you read from a file. I also want to print my output to the same name of the file plus ".txt". So if my file is called "text" I want to print it to a file called "text.txt".

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

int main(int argc, char* argv[1])
{
    FILE* Data2;
    Data2 = fopen(argv[1], "r");
    if (Data2 == NULL)
    {
       printf("ERROR");
       exit(100);
    }

    return 0;
}

Would it be something like this? Is it better to use "a" or "w" in the fopen? I want to write to it several times.

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

int main(int argc, char* argv[1])
{
    FILE* Data2;
    FILE* Data3;
    Data2 = fopen(argv[1], "r");
    Data3 = fopen(argv[1].txt, "a");
    if (Data2 == NULL)
    {
        printf("ERROR");
        exit(100);
    }
    fprintf(Data3, "\n Print something \n");
    fprintf(Data3, "\n Print something \n");
    fprintf(Data3, "\n Print something \n");

    return 0;
}
COKEDUDE 27 Junior Poster in Training

What does creating a pointer of a function do? I'm not used to getValue being a pointer.

char* getValue(char* string);

I'm used to something like this.

char getValue(char* string);
COKEDUDE 27 Junior Poster in Training

Here is my code that is doing the printing that gets messed up. The output is fine when you display it to your screen (terminal). Unfortunately when you redirect it to a text file it gets messed up. It displays the null characters (^@) and Enquiry characters (^E). I have to use a for loop like this or my outputs gets messed up. I can't use the %s option or my output won't be in the way I need it.

FILE *input;
char line[80] = {0};
while(fgets(line, 80, input) != NULL)
{
    if((int)line[0] == 46)
    {
        //printf("You have a period \n");
        for(i = 31; i < 80; i++)
        {
            printf("%c", line[i]);
        }
        //printf(" ------------------------\n");
        memset(line, 0, 80);
        comment_flag = 1; 
    }
}
COKEDUDE 27 Junior Poster in Training

I looked at the man pages and saw those were the maximum values of usleep() and nanosleep(). With sleep I thought I would get a normal 5 seconds. I am working on a modified version of game of life. It is hard to see if it is doing what I want with how fast it is going.

usleep(1000000);
nanosleep(999999999);
delay(3000);
sleep(5);
COKEDUDE 27 Junior Poster in Training

Is there a good way to slow down program output? usleep and nanosleep don't slow it down enough, delay doesn't work, and sleep keeps freezing my program. I am using Linux since I think this makes a difference in what I have to use.

COKEDUDE 27 Junior Poster in Training

Could you elaborate on that please? I thought those are 2 separate things. I thought the first one is declaring a char pointer to stdup. I thought the second one is const char pointer to source where you can't change it (which I need to add values to it as I run my program) and you can make a string a string array that can't change.

char* strdup
const char* source

These are your 2 choices that I am familiar with. I like the first one because its more efficient. Since I'm using C I like to be efficient :).

strs[i] = malloc(strlen("foo") + 1);
strcpy(strs[i], "foo");

char *strs[NUMBER_OF_STRINGS][STRING_LENGTH+1];
COKEDUDE 27 Junior Poster in Training

I am using fgets to read lines of a file. I am then using if statements based on the location of the new line character to decide which sscanf to use. I have been getting a lot of windows files which usually use carriage returns. Most of the timeit "seems" to work ok with carriage returns but other time it goes crazy on me. I was wondering how I should handle this. Should I add the ascii value of carriage returns to my if statements? Or is there a better way to handle this?

if ( line[10] == 10 )
{
    //sscanf
}

if ( line[20] == 10 )
{
    //sscanf
}

if ( line[30] == 10 )
{
    //sscanf
}
COKEDUDE 27 Junior Poster in Training

I figured it out. I didn't allocate memory.

COKEDUDE 27 Junior Poster in Training

I am trying to copy a string to an array of string. I have used these two examples before and they have worked so I don't understand why they won't work this time. I am getting a segmentation fault.

http://stackoverflow.com/a/1088667/985898
http://stackoverflow.com/a/1095006/985898

I remembered to initialize everything.

char *strings_mneumonic_table[503] = {0};
char mneumonic[20] = {0};
int start_address = 0;
int hash = 0;
        if(line[0] == 32)
        {
            printf("32 group \n");
            sscanf(line, "%s %x", mneumonic ,&start_address);
            printf("mneumonic is %s\n", mneumonic);
            printf(" hash is %d \n", hash);
            strcpy(strings_mneumonic_table[hash], mneumonic);
            //printf("under strcpy(strings_mneumonic_table[hash], mneumonic); \n");
            //hex_address_table[hash] = start_address;
            //printf("hex_address_table[hash] = start_address; \n");
            //printf("end of 32 group \n");
        }

Here is my output

hash is 2 
little start 
BIG START 
32 group 
mneumonic is START
hash is 2 
Segmentation fault (core dumped)
COKEDUDE 27 Junior Poster in Training

I wanna do everything in Hex. The only 2 ways to convert is to make an array which you can't do math with a char array and the %x option with printf and then you can't do math that way.

COKEDUDE 27 Junior Poster in Training

How would you go about converting a decimal value to hex and then do math? Every example of converting decimal to hex that I have seen creates an array and I wouldn't be able to do math if I did that. Something like this.

15 decimal to hex F
17 decimal to hex 11
F hex + 11 hex = 20
COKEDUDE 27 Junior Poster in Training

Yes :). Marking as solved ;).

COKEDUDE 27 Junior Poster in Training

I am trying to clear the first character of my array. The first two methods don't seem to work. The third method seems to but doesn't look like a good idea.

char operand[] = "abc";
//both methods seem to null it out
operand[0] = 0;
memset(operand, 0, 1);
printf("%s", operand);


//seems to work
memset(operand, 5, 1);
COKEDUDE 27 Junior Poster in Training

This did what I want.

char *pch;
char string[] = "Fishing tourism miami atlanta dallas houston";
pch = strstr (string,"houston");
if(pch != NULL && start_flag != 1)
{
    //Do Work
}
COKEDUDE 27 Junior Poster in Training

I have an array of characters. I am trying to find "houston". The only way I can think of to do this is to use a for loop and check each individual character. Is there an easier way to do this?

char string[] = "Fishing tourism miami atlanta dallas houston";
COKEDUDE 27 Junior Poster in Training

They have #defines at the top.

#define for_x for (int x = 0; x < w; x++)
#define for_y for (int y = 0; y < h; y++)
#define for_xy for_x for_y

The initialization part also looks different than what I am used to. I am used to this with int.

int point[6]={0,0,1,0,0,0};

And this with char.

char string[] = "Merkkijono";
char string[] = {'M', 'e', 'r', 'k', 'k', 'i', 'j', 'o', 'n', 'o', '\0'};
COKEDUDE 27 Junior Poster in Training

I'm trying to understand some code on rosetta stone and I thought actually understanding that library would help.

COKEDUDE 27 Junior Poster in Training

Can someone please explain what unistd.h does and the purpose of it? I have been googleing the last hour and I don't understand it.

COKEDUDE 27 Junior Poster in Training

Can you please explain what this weird int declaration does. Here is the block of code.

void show(void *u, int w, int h)
{
    int (*univ)[w] = u;
    printf("\033[H");
    for_y {
    for_x printf(univ[y][x] ? "\033[07m  \033[m" : "  ");
    printf("\033[E");
    }
    fflush(stdout);
}

This is the line I'm talking about.

int (*univ)[w] = u; 
COKEDUDE 27 Junior Poster in Training

Thank you :). It worked :).

COKEDUDE 27 Junior Poster in Training

I want to use strcmp on my array of string. I want to know if my value is already in my array. I obviously can't do that on my null values or I will have a segmentation fault. So since I know the ascii value of Null is 0 I was preventing it from reaching the strcmp with the cast.

I removed all of my int casts and I still get a segmentation fault. Should I also chang all my "0" to NULL?

for(i = 0; i < 50; i++)
{
    //printf(" i is %d \n", i);
    if(strings[i] != 0)
    {
        //printf(" Top if 1 \n");
        /*if(val == 0)
        {
            printf(" val is %d so breaking \n", val);
            break;
        }*/
        if(strcmp (strings[i], nam) == 0)
        {
            //printf(" strings[i] is equal\n");
            //printf(" strings[i] is %s \n", strings[i]);
            //printf(" nam is %s \n", nam);
            string_equal_flag = 1;
            break;
        }
        //printf(" Top if 2 \n");
    }
    if(i == 49)
    {
        string_not_flag = 1;
        //printf(" set string_not_flag\n"); 
    }
}
COKEDUDE 27 Junior Poster in Training

Can someone please explain this error and how to fix it? This worked perfectly on my computer but it stopped working when I transferred it to another computer.

int hash = 0; 
char *strings[100];
if((int)strings[i] != 0)
if((int) strings[hash] != 0)
while((int) strings[hash] != 0)
if((int)strings[hash] != 0)
if((int)strings[hash] != 0)
COKEDUDE 27 Junior Poster in Training

Can someone please tell me what is wrong with strcmp? I don't understand why I am getting a segmentation fault when comparing array of strings and string.

    char *strings[100];
    char nam[100];
    int g = 0;
    while (fscanf(pFile, "%s %d",  nam, &val) !=EOF)
    {

        strings[k] = nam;
        printf(" string is %s .\n", strings[k]);
        k++;
        g = strcmp (strings[i], nam);
        printf("g is %d \n", g);
    }
COKEDUDE 27 Junior Poster in Training

Can I please have a good detailed explanation on the differences between malloc and calloc? I always have trouble understanding that.

COKEDUDE 27 Junior Poster in Training

Can someone please tell me the the maximum and minimum of int, long int, long long int, double, float, and anything bigger than that? And also how to calculate this?

COKEDUDE 27 Junior Poster in Training

Which file readers in C can handle reading an inconsistent file? Sometimes the file is "word number" and other times it is just "word". Like this.

bob 456
echo
cat 
dog 1101
peacock 300

This is what I tried with fscanf. I am surprised it worked. I didn't think fscanf liked inconsistent files. Is there something I need to be worried about? I know fscanf has really bad side effects if you are not careful.

while (fscanf(pFile, "%s %d",  nam, &val) !=EOF)
{
    //my work
}
COKEDUDE 27 Junior Poster in Training

Is this a bad idea?

COKEDUDE 27 Junior Poster in Training

I am trying to use fscanf to read a file. When fscanf hits a newline I would like it to do one thing and when it hits a space I would like to do something else. Is this hopefully possible?

char nam[100];
while (fscanf(pFile, "%s",  nam) !=EOF)
{
    if(space)
    //do something
    if(newline)
    //do something
}
COKEDUDE 27 Junior Poster in Training

I've seen a few different ways to convert a string to ascii and I was wondering what is the best way and why.

char str[100];
    int i=0;

    printf("Enter any string: ");
    scanf("%s",str);

    printf("ASCII values of each characters of given string: ");
    while(str[i])
         printf("%d ",str[i++]);

-----------------------------------------------------------------------------

int main()
{
 char *s="hello";
 while(*s!='\0')
  {
  printf("%c --> %d\n",*s,*s);
  s++;
  }
 return 0;
}

-----------------------------------------------------------------------------

char word[32];
int x = 0;
for ( x = 0; word[x] != '\0'; x++ )
{
}

-----------------------------------------------------------------------------

int main (void)
{
    char string[50];
    int i, sum;
    scanf("%s", string);
    sum = 0;
    for(i = 0; i < strlen(string); i++)
        sum += (int)string[i];

    printf("(%d)\n", sum);
    system("pause");
    return 0;
}

-------------------------------------------------------------------------------

    int num=0;
    while (word[x] != '\0') // While the string isn't at the end...
    {
    num+=int(word[x])
    x++;
    }

I like the last while loop the most but I think I will use the last for loop. I will be reusing my array of char with fscanf %s multiple times. I think strlen can handle this better. Will the while loop get confused by multiple NULL characters or will fscanf be able to overwrite the NULL character each time. When I tested it with the last for loop it seemed to work correctly.

COKEDUDE 27 Junior Poster in Training

@Ancient Dragon

The reason for modding it is I want to use a hash table to check if I have already stored a value. If I have I will need to deal with collission. I'm trying to break this into small pieces :).

This was the output after I read 25 from a file. I was planning to fix the while loop after I fix the reading issues. I don't understand how 25 turns into 2,50,5,53.

 C is 2 .
 C is 50 .
 C is 5 .
 C is 53 .
 C is 

@Banfa
500 sounds good to me :). Thats also good that you can reuse the code.

@deceptikon
Thank you for explaining that.

COKEDUDE 27 Junior Poster in Training

@Ancient Dragon
Can you tell me what the single quotes are for with '0'? I was using an int. I thought the single quotes are for char.

I intend to add the numbers then store them in an array. For the char I intend to add the ascii value of each char and mod them, then store them in an array.

How would I go about reading an entire line at a time? Not all lines will have numbers so is that possible?

Can you tell me why this caused a big mess? My plan was just to add a single number at a time to an array then use atoi to convert it to an integer. Unfortunately I was never able to get to that step. I tried to use the simple number of 25. Unfortunately it seems fgetc really doesn't like numbers. Somehow it turned 25 into 2,50,5,53. I really don't understand how this is possible. I tried with single quotes around my 0 and 9 since I have seen it done both ways.

while ((c = fgetc(pFile)) != EOF)
{
        printf(" C is %c .\n", (char)c);
        printf(" C is %d .\n", c);
        if (c >= '0' && c <= '9' )
        {
            //next one
            printf("third if.\n");
            char int_holder[100];
            int i = 0; 
            while(c >= '0' && c <= '9')
            {
                //start
                int_holder[i++] = c;
                //printf("i is %d \n", i);  
            }
            sum = atoi(int_holder); 
            printf("The sum is %d \n", sum);
            int_holder[i++] = '\0';
            printf("int_holder is %s …
COKEDUDE 27 Junior Poster in Training

Is there a way to read a file character by character for chars and the number set for numbers?

bob 4567
joe 39083
sara 4239824

That is my file

while ((c = fgetc(pFile)) != EOF)
{
    //my work
}

I know this works on my chars. This won't read numbers in the I want. This reads b,o,b,space,4,5,6,7. I want it to read b,o,b,space,4567. The reason for this is I will be adding the 4567 plus 39083 plus 4239824 eventually.

COKEDUDE 27 Junior Poster in Training

And if you need a char you always cast it like this :).

while ((c = fgetc(pFile)) != EOF)
{
    vertices[addcounter] = (char)c;
}
COKEDUDE 27 Junior Poster in Training

I am trying to print an array of linked lists. I am having trouble getting it to print. Here is my struct.

typedef struct VERTEXTAG
{
    char c;
    bool isvisited;
    struct EDGETAG* p;
}VERTEX;

typedef struct EDGETAG
{
    VERTEX* v;
    struct EDGETAG* q;
    //cookies rock
    //I like cookies
}EDGE;

Here are my variable declarations

VERTEX v[100];
EDGE *e;
EDGE* temp;
int counter = 0;
int addcounter = 0;
int i = 0;

Here is where I try to create the linked lists. I have an even case and an odd case.

                //even case
                if(counter - i == 1 && flag == 0)
                {
                    vertices[addcounter] = (char)c;
                    //printf("The vertice is %c :\n", vertices[addcounter]);
                    e = (EDGE*) malloc(sizeof(EDGE));
                    v[addcounter].p=e;
                    v[addcounter].c= (char)c; 
                    v[addcounter].isvisited=false;
                    v[addcounter].p=NULL;  
                    addcounter++;
                }

                //odd case
                if(counter - i == 1 && flag == 0)
                {
                    vertices[addcounter] = (char)c;
                    //printf("The vertice is %c :\n", vertices[addcounter]);
                    e = (EDGE*) malloc(sizeof(EDGE));
                    v[addcounter].p=e;
                    v[addcounter].c= (char)c;
                    v[addcounter].isvisited=false;
                    v[addcounter].p=NULL; 
                    (*e).v= &v[addcounter];
                    e->q = NULL;
                    addcounter++;
                }

Here is where I try to print my linked list. For some reason temp is equal to NULL so it is not printing. I know I am correctly passing my variables to each case with vertices array. It prints out correctly. I am not sure if I am correctly creating the linked list of arrays since it will not print out.

temp = v[0].p;
if(temp == NULL)
{
    printf("Temp is Null\n");
}

while(temp != NULL)
{
    printf("While loop");
    printf("%c", (*(*temp).v).c);
    temp = …
COKEDUDE 27 Junior Poster in Training

Thank you :). Were both saying the same thing but can't explain it in a way the other understands.

COKEDUDE 27 Junior Poster in Training

My understanding of fgetc is it reads character by character of a file. They think fscanf can also read character by character of a file. I do not think that is correct. I think fscanf is used for reading an entire file. I may not be explaing that with the correct terminology but I have used it for reading an entire file before.

COKEDUDE 27 Junior Poster in Training

What is the difference between fscanf and fgetc? I have a few people arguing with me on the differences. I thought fgetc reads character by character of a file. I thought fscanf reads the whole file. Is that corrrect? Can someone elaborate on what I forgot?

COKEDUDE 27 Junior Poster in Training

Is there a way to check the default version of C89, C90, or C99 that gcc uses to compile your programs? I tried -v and --version but they only give the actual package of gcc info.

COKEDUDE 27 Junior Poster in Training

When doing a linked list of chars with just a single character do you think it's better to use pointers in your struct like this?

Typedef struct String_Node
{
    Struct String_Node* next;
    Struct String_Node* prev;
    int id;
    char* c;
} String_Node_t;

Or better to not use a pointer with the char like this?

Typedef struct String_Node
{
    Struct String_Node* next;
    Struct String_Node* prev;
    int id;
    char c;
} String_Node_t;
COKEDUDE 27 Junior Poster in Training

@deceptikon
I'm sorry I can't because it gives a segmentation fault. And it happens right where I try to get the strlen and sizeof. These are the warnings if they help.

gcc -Wall *.c
open_file_c1.c: In function ‘main’:
open_file_c1.c:151:5: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:361:12: note: expected ‘const char * restrict’ but argument is of type ‘unsigned int’
open_file_c1.c:152:5: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:361:12: note: expected ‘const char * restrict’ but argument is of type ‘int’

How do you quote people I don't see a way to do that.

@Ancient Dragon
I'm trying to get something similar to "hello" like you mention in your second post. How would I get the length of the string? I know I have a valid string because of this. I'm able to print through with a for loop and print it as a string.

for(i = 0; i < 100; i++)
{
    printf("The vertice is %c :\n", vertices[i]);
}

printf("The vertice is %s :\n", vertices);
COKEDUDE 27 Junior Poster in Training

Sorry I thought it was a simple pointer issue so I didn't elaborate very much. I first used malloc to allocate some space. Then I use this if statement to fill vertices with some char values. After that I add a null character at the end so it is properly terminated like char arrays are supposed to be.

vertices = malloc(1000);
if(counter - i == 1 && flag == 0)
{
    vertices[addcounter] = (char)c;
    //printf("The vertice is %c :\n", vertices[addcounter]);
    addcounter++;
}
vertices[addcounter] = '\0';
COKEDUDE 27 Junior Poster in Training

I am trying to calculate the size of char *. I would would think one of these 2 methods would work but they are not.

char *vertices;
printf(strlen(vertices));
printf(sizeof(vertices));
COKEDUDE 27 Junior Poster in Training

I am trying to print a char array on separate line for each element. This is what I have tried and neither way is working. I would like the output to look something like.

The array length is c c
The array length is o o
The array length is d d
The array length is e e

char str1[5] = "code";
char str2[5] = "code";
for(i; i < 10; i++)
    {
        printf("The array length is %c %c :\n", str1[i], str2[i]);

    }   

char str1[5] = "code";
char str2[5] = "code";
for(i; i < 10; i++)
    {
        printf("The array length is %s %s :\n", str1[i], str2[i]);

    }   
COKEDUDE 27 Junior Poster in Training

Can someone please tell me why I keep getting a stack overflow when I try to create nodes in a priority queue?

class Node
   {
   public int frequency;              // data item (key)
   public char character;           // data item
   public Node leftChild;         // this node's left child
   public Node rightChild;        // this node's right child
   public HuffmanTree leaf;
   public HuffmanLeaf leaf1; 
   //LinkedList<Integer> bob;
   //bob = new LinkedList<Integer>();
   PriorityQueue<Node> trees;
   public Node()                  //Constructor
   {
   }
   public Node(int passed_frequency, char char_character)                  //Constructor
   {
       //super(frequency);
       System.out.println("Start of Node(int passed_frequency");
       /*for (int i = 0; i < 1; i++)
       {
           //System.out.println("passed frequency " + passed_frequency +
                   //" char_character " + char_character);
       }*/
       int work = passed_frequency;
       char grr = char_character;
       trees.offer(new Node(work, grr));
       System.out.println("--------------------------------------------");
       //break;
   }
}
COKEDUDE 27 Junior Poster in Training

I would like to create an incremented variable by how many passes I go through my for loop. This is what I was thinking. I would like my variable to total with the pass number at the end. So the first pass would be total0, followed by total1, total2, total3, total4. What would you have to do to make the value usuable in the rest of your class? I noticed it would not work outside my for loop.

    int[] array = new int[]{11, 8, 7, 6, 5};
    int[] anArray = new int[100];
    //int[] array = null;
    int total = 0;
    int x = 0;


        for (int i = 0; i < 5; i++)
        {
            //System.out.println("The values in the array ");
            anArray[i] = array[i]; 
            int totali = array[i];
            System.out.println("totali " + totali);
        }
        System.out.println("totali " + totali);
        System.out.println("The length of the array is " + anArray.length);
        for (int i = 0; i < anArray.length; i++)
        {
            if(anArray[i] != 0)
            {
                System.out.println("The values in the array " + anArray[i]);
            }
        }
COKEDUDE 27 Junior Poster in Training

I would like to print my values in my array. I am filling my values with a string tokenizer that I leave up to the user to fill.

When I use this method I get extra values since its initialized to 10.

int[] anArray = new int[10];
for (int i = 0; i < anArray.length; i++)
            {
                System.out.println("The values in the array " + anArray[i]);
            }

When I use this method it gives me a null error. If I don't initialize it complains that I haven't intialized it.

int[] array = null;
for (int i = 0; i < anArray.length; i++)
            {
                System.out.println("The values in the array " + anArray[i]);
            }

What can I do to print the values filled values in array? 
COKEDUDE 27 Junior Poster in Training

You should also check out this website, especially if vb.noob.
http://www.homeandlearn.co.uk/NET/vbNet.html

Thank you. I really like this website.

COKEDUDE 27 Junior Poster in Training

Does anyone have any good .NET Tutorials? What are some good websites for .NET Tutorials? Sorry if this is in the wrong section. I don't know enough about .NET to ask in the right section.

COKEDUDE 27 Junior Poster in Training

This does a nice job of explaining headers.

http://www.codeguru.com/forum/showthread.php?t=344569

COKEDUDE 27 Junior Poster in Training

Whats the difference between using CC and CPP as the file extension for C++ programs? I thought C++ use the CPP extension for source files. I was reading this article and saw he used CC.

http://community.linuxmint.com/tutorial/view/111