C :: How To Make Strncpy With Fgets And Pointers
Mar 31, 2013
Why will this code not work? It cashes the program on me once i get to it...
Code:
memset(&input, 0, sizeof(input));
printf("Enter your firstname: ");
pinput = fgets(pinput, BUFF, stdin);
strncpy((pct + *pctcounter-1) ->firstName,pinput , strlen(pinput) - 1);
View 5 Replies
ADVERTISEMENT
Apr 13, 2014
How can I make fgets stop reading when it reaches a new line? Right now it will read the new line and continue until the buffer is full. I was thinking something like this.
Code:
while(fp!='
'){
fgets(password, 256, fp);
}
View 2 Replies
View Related
Nov 5, 2014
I need a function like strncpy; but, want to be sure the destination ends with a ASCII nul char.
So, I wonder what name is used by others in this case.
I have found strlcpy and it looks like a good name choice. [URL] ....
View 5 Replies
View Related
Sep 4, 2013
Currently I am doing the first exercises from Illustrating C. The exercise that I am trying first is the one where someone can input degrees and the program will be able to put those in to sin or cos. Im trying to use fgets to take input from the user. the answer can only be sin or cos. Im having trouble with how to get it to work.
My goal is to have the output of the choice sin or cos. Store that choice. proceed to ask what the degrees are from the user. then i would have the degree input multiplied by pi/180 converting it to radians and having the program compute it that way
Code:
#include <stdio.h>
#include <math.h>
#include <string.h>
int a;
int main()
}
[code]....
View 11 Replies
View Related
Dec 11, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COL 70
#define MAX_ROW 20
}
[code]...
With my input.txt file being Code: abcd efgh And in particular, there is no new line after the letter h, but when I print out the text string, I get a new line after h. Why is this?
View 5 Replies
View Related
Aug 31, 2014
I am trying to write a terminal-like chat application in Linux. I would like to use a FIFO queue to print out the messages in terminal. The queue would be populated from 2 sources- stdin and messages sent from the other user over TCP. I have meet an obstacle that I cannot handle...
Lets say I would like to take user input using fgets and put it into a buffer. Then queue it if the buffer is not empty or print if it is. The problem is that when I use fgets or scanf, my input is instantly printed to the terminal..If i do:
Code:
fgets(message, 100, stdin); printf
("%s", message The string under message is printed twice :|. Is there a way to prevent this?
View 5 Replies
View Related
Apr 20, 2013
Code:
char line[BUFSIZ];
while ( fgets(line, sizeof line, file) != NULL ){
llen = strlen(line);
printf("%d - %s
",llen,line);
}
I get a full line printed but my llen is the size of my buffer. how do i get the total size om my line from the beginning to " "
View 3 Replies
View Related
Dec 21, 2014
Is it possible to fgets() the string (or word) only after a delimiter? I yes then how?
Example: Code: printer, scanner, machine
Also, how can I sscanf() a string with an indefinite number of sizes and assign it to only one variable?
Example:
Code:
str = "I Love C programming 9000";
sscanf(str, "%s %d", strvar, intvar);
View 13 Replies
View Related
Oct 12, 2014
Intead of using scanf("%d",&a) to take a input from the user,how to take a input using fgets(buffer, BUFFERSIZE , stdin) and atoi?
View 1 Replies
View Related
Mar 12, 2013
i am trying to read a string using fgets and storing in an array i want to prevent fgets from storing the new line character on the array using the shortest means possible..
View 2 Replies
View Related
Sep 16, 2013
I have prepared a file through the use of following code
Code:
fprintf(file2, "%i %i %i %i %i %i
",
msg_id,
msg_size,
msg_period,
msg_deadline,
msg_producer,
msg_comsumer
);
As one can see, this file has tab separated integer entries. The file is written correctly, let us call this file "msg.txt".
I face the problem when I read this file, using fgets as follows:
Code:
char singleMessage[100];
while( fgets(singleMessage, sizeof(singleMessage), file ) )
{
puts(singleMessage);
sscanf(singleMessage, "%i %i %i %i %i %i
",
&first, &second, &third, &fourth, &fifth, &sixth);
fprintf(stderr, "first: %d, second: %d, third: %d, fourth: %d, fifth: %d, sixth: %d
",
first, second, third, fourth, fifth, sixth);
}
but fgets only retrieves until the first, i.e, if the first line in the file reads:
788004425
fgets returns only 78.
Does it have to do with how the file was written in the first place.
View 1 Replies
View Related
Jul 2, 2014
How can i play a mp3 file in the background?How can i rule the volume of the mp3 file?why does delay(0.25) doesnt work in this code?
Code:
FILE *datei;
char line[ANZAHL][LAENGE];
int i ;
datei = fopen ("song.txt", "r");
if (datei != NULL){
for(i = 0; i < ANZAHL;i++) {
delay(0.25);
fgets(line,LAENGE,datei);
printf ("%s
[code]....
View 2 Replies
View Related
May 21, 2014
I'm new to C/C++. I am attemping to use fgets and sscanf to read a line of input, and confirm it is a positive number.My code works great, except for the case of a negative number. When I enter a negative number, my while loop seems to run infinitely, with stdin providng the same input over and over again.
Here's the code snippet:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TEXT_LEN 64
void foo() {
char* inStr = (char*)malloc(sizeof(char)*TEXT_LEN);
memset(inStr, 0, TEXT_LEN);
}
[code]....
View 9 Replies
View Related
Oct 31, 2014
#include<stdlib.h>
#include<stdio.h>
#include <string.h>
int main() {
char input[256];
char buffer[256];
[Code] ....
The output of this short program is really weird. When I type 123 from the keyboard,I get the following answer in console window"123 length=4".Why the output of length is always 1 more than the actual length of the string that I type in.
View 2 Replies
View Related
May 27, 2013
I am trying to use fgets to read in a string, but keep getting a "no conversion function from std::string to char" error.
View 2 Replies
View Related
Feb 17, 2013
In the assignment we are forbidden to use fscanf(). I have been trying to get this to work, but I've started to realize that I do not have a complete understanding of what strtok() actually does. I'm getting this warning when debugging: "assignment makes integer from pointer without cast."
This warning happens when assigning str to goal and assist, and I think it is because they are, when dereferenced, integers. The code below correctly assigns the name into the correct spot, but leaves nonsense data in the goal and assist arrays.
ex:-7880, -7888 file example: NAME GOALS ASSISTS JOHN 1 2
Code:
void readLinesFromFile( FILE* Ptr, int* goal, int* assist, char** name, int lines ){/*
* Reads lines from files and populates the arrays with the corresponding info.
*/
int index;
char hold[ MAX_LINE ] = { 0 };
char* str = NULL;
[Code] .....
From what I understand about strtok(), it returns a string, and takes in a character array and a key value that tells it when to stop. In the online examples I've seen, they use NULL in the first field. I'm not sure why.
View 5 Replies
View Related
Feb 28, 2014
I'm trying to create an array of pointers to pointers which will point to array of pointers (to strings) I tried
Code:
int i;
char *string[]={
"my name is dave",
"we like to dance together",
"sunny day",
"hello",
[code]...
the app keeps crashing , I don't know how to make the array-elements to point to another array-elements..
View 4 Replies
View Related
May 21, 2013
I am a little confused while comparing char pointers to integer pointers. Here is the problem:
Consider the following statement;
char *ptr = "Hello";
char cArr[] = "Hello";
When I do cout << ptr; it prints Hello, same is the case with the statement
cout << cArr;
As ptr and cArr are pointers, they should print addresses rather than contents, but if I have an interger array i.e.
int iArr[] = {1, 2, 3};
If I cout << iArr; it displays the expected result(i.e. prints address) but pointers to character array while outputting doesn't show the address but shows the contents, Why??
View 2 Replies
View Related
Jan 22, 2014
I have this
Code:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int check_up(char string[]);
int check_low(char string[]);
void to_up(char string[]);
void to_low(char string[]);
[Code] .....
When I compile this I have the following problems: warning: data definition has no type or storage class [enabled by default] in 'to_up(word)'conflicting types in 'to_up' function and to_low function warning: data definition has no type or storage class [enabled by default] into_up function error: unknown type name "word" in line 'printf("All uppercase %s. ", word):;'warning: parameter names (without types) in function declaration [enabled by default] in 'to_up(word)'and 'to_low(word)' 'note: previous declaration of "to_up" was here in function declaration of to_up function
View 7 Replies
View Related
Nov 5, 2014
I'm using fgets which will read a single line at a time, but unlike fgets I don't want it to return the new line char ( ) ?I want to be able to use printf to display the lines next to each other.
View 7 Replies
View Related
Apr 15, 2013
I'm trying to write a function that takes a 32bit address and a data to store at this address.
I'm wanting to take the 32 bit memory address eg 0x12345678 and split it
into 4 x 2 bytes
12, 34, 56, 78
then each of the 4 entries is at most a 256 entry array.eg
FF, FF, FF, FF
So in this example, 0x12 points to 0x34 in the second array, which points to 0x56 in the third array, which finally points to 0x78 in the last array. This last array holds the actual data.
After successfully doing 0x12345678, say I might get a read for 0x1234AABB. So, the first and second pointers already exist, but I then have to create and write to dynamically created arrays.
The arrays need to have all entries set to NULL so that i know whether to follow the pointers to overwrite a previously entered value or create new arrays and pointers.
It all looks good and simple in the pseudo code I've written up but I'm having trouble coding it. I'm currently trying to deal with the first entry case, ie all array elements are NULL, but I'm getting confused with the pointers and creation of new arrays.
void cpu::store(unsigned int mem_add,unsigned int mem_val) {
int first = (mem_address&4278190080)>>24;
int second = (mem_address&16711680)>>16;
int third = (mem_address&65280)>>8;
int fourth= (mem_address&255);
[Code] .....
A1 has been declared as
int* A1[256] ;
View 3 Replies
View Related
Apr 4, 2013
give me a few solid examples for when one should use pointers.
View 14 Replies
View Related
Mar 3, 2013
The first sample program that I am reading on the book has the following code:
Code:
* Demonstrates basic pointer use. */
#include <stdio.h>
/* Declare and initialize an int variable */
int var = 1;
}
[code]....
Is this a compiler error or is there a proper syntax for pointers using the gcc compiler?
View 3 Replies
View Related
Dec 24, 2013
So when if you want to change the values themselves for example in an array (just for example) this is valid;
void test(int* test)
{
test[0] += 10;
}
int bob[] {1, 3, 5};
If you did that bob[0] would not equal 11. All well and good right?
Now if you do this?
int sally = 33;
test(sallay);
This wouldn't work at all you actually have to use
void test(int& test)
{
test += 10;
}
how the memory addresses etc. are working here? I don't understand why you need to use & the reference operator if it's not an array? Why wouldn't that still work?
View 3 Replies
View Related
May 15, 2014
Alright, after what seems like forever I'm on the last stage of modifying my Payroll Program. This time I have to sort using pointers and I only have to sort the Net Pay category.
Using one of my earlier programs I've removed content from it until it's in a state where it runs but contains no sorting functions.
So now I've to a working program that lacks any kind of sorting and looks like this.
#include <iostream>
#include <fstream>//file input output stream
#include <iomanip>
#include <string>
using namespace std;
//function prototypes
int readalldata(long int id[], string fname[], string lname[], int hoursworked[], float hourlyrate[], int n);
[Code] .....
My hints for how to add pointers to the program are...
int *np,tmp;
for (i=0,i<n; i++) np=&netpay[i]; //associating the netpay to np;
temp=np[i];//storing the pointer to a temp
I've been fooling around trying to sort the Net Pay by pointers using these hints for a bit now and I'm still not getting it.
View 2 Replies
View Related
Sep 6, 2013
I have a problem. I want to set pointers to a default value for both 32 bit and 64 bit compiles. The 32-bit version works as:
enum constants { UNDEFINED = 0xDeadBeef }
if ((unsigned long)ptr == UNDEFINED)
but I can't seem to extend this to 64-bits. I've tried
#if __SIZEOF_POINTER__ == 4
enum constants { UNDEFDATA = 0xDeadBeef };
}; // enum constants
#elif __SIZEOF_POINTER__ == 8
enum constants { UNDEFDATA = 0xDeadBeefDeadBeef };
#endif
with:
if (ptr == UNDEFINED)
but get a message saying the '==' is undefined (I understand this)
Is there any way to setup so that I can change the size of my constants so that the comparisons will always work correctly? I've tried a 'typedef' but the compiler complains at
'typedef unsigned long long ADDR' // won't accept, and
static const SlipCellBase * const TEMPORARY = (SlipCellBase&)0xFFFFFFFFFFFFFFFF; // illegal conversion
enum doesn't work (because it's an int?)
View 1 Replies
View Related